LintCode: Binary Tree Postorder Traversal

简介:

C++,递归

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14     /**
15      * @param root: The root of binary tree.
16      * @return: Postorder in vector which contains node values.
17      */
18 public:
19     vector<int> postorderTraversal(TreeNode *root) {
20         // write your code here
21         vector<int> result;
22         if (root == NULL) {
23             return result;
24         }
25         if (root->left != NULL) {
26             vector<int> left = postorderTraversal(root->left);
27             result.reserve(result.size() + left.size());
28             result.insert(result.end(), left.begin(), left.end());
29         }
30         if (root->right != NULL) {
31             vector<int> right = postorderTraversal(root->right);
32             result.reserve(result.size() + right.size());
33             result.insert(result.end(), right.begin(), right.end());
34         }
35         result.push_back(root->val);
36         return result;
37     }
38 };
复制代码

C++,递归,辅助函数

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14     /**
15      * @param root: The root of binary tree.
16      * @return: Postorder in vector which contains node values.
17      */
18 public:
19     vector<int> postorderTraversal(TreeNode *root) {
20         // write your code here
21         vector<int> result;
22         if (root == NULL) {
23             return result;
24         } else {
25             postorderCore(root, result);
26         }
27         return result;
28     }
29     void postorderCore(TreeNode *root, vector<int> &result) {
30         if (root == NULL) {
31             return;
32         }
33         if (root->left != NULL) {
34             postorderCore(root->left, result);
35         }
36         if (root->right != NULL) {
37             postorderCore(root->right, result);
38         }
39         result.push_back(root->val);
40         return;
41     } 
42 };
复制代码

C++,非递归

[一个stack]

[一个cur指针]

[一个pre指针]

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14     /**
15      * @param root: The root of binary tree.
16      * @return: Postorder in vector which contains node values.
17      */
18 public:
19     vector<int> postorderTraversal(TreeNode *root) {
20         // write your code here
21         vector<int> result;
22         if (root == NULL) {
23             return result;
24         }
25         
26         TreeNode *cur = root, *pre = NULL;
27         stack<TreeNode *> sta;
28         
29         while (cur != NULL || !sta.empty()) {
30             while (cur != NULL) {
31                 sta.push(cur);
32                 cur = cur->left;
33             }
34             cur = sta.top();
35             if (cur->right == NULL || cur->right == pre) {
36                 sta.pop();
37                 result.push_back(cur->val);
38                 pre = cur;
39                 cur = NULL;
40             } else {
41                 cur = cur->right;
42             }
43         }
44         return result;
45     }
46 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4999458.html,如需转载请自行联系原作者

相关文章
|
数据采集 供应链 搜索推荐
分享116个PHP源码PHP源码,总有一款适合你
分享116个PHP源码PHP源码,总有一款适合你
292 0
|
11月前
|
存储 关系型数据库 数据库
RHCA认证学习分享
RHCA认证学习分享
288 5
|
安全 Linux
18.4 SELinux配置文件(/etc/selinux/config)
我们知道,SELinux 是预先配置的,可以在不进行任何手动配置的情况下使用 SELinux 功能。然而,一般来说,预先配置的 SELinux 设置很难满足所有的 Linux 系统安全需求。
796 0
18.4 SELinux配置文件(/etc/selinux/config)
|
3天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
755 17
|
11天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
783 59
Meta SAM3开源:让图像分割,听懂你的话
|
1天前
|
人工智能 安全 小程序
阿里云无影云电脑是什么?最新收费价格个人版、企业版和商业版无影云电脑收费价格
阿里云无影云电脑是运行在云端的虚拟电脑,分企业版和个人版。企业版适用于办公、设计等场景,4核8G配置低至199元/年;个人版适合游戏、娱乐,黄金款14元/月起。支持多端接入,灵活按需使用。
231 164

热门文章

最新文章