LintCode: Binary Tree Postorder Traversal

本文介绍使用C++实现二叉树的后序遍历,包括递归和非递归两种方法。递归方法通过直接调用自身完成遍历,而非递归方法则借助栈来迭代地遍历节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值