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 };
时间: 2024-12-21 09:16:32

LintCode: Binary Tree Postorder Traversal的相关文章

[lintcode easy]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Challenge Can you do it without recursion? Among preorder,inorder and postorder binary tree

42: Binary Tree Postorder Traversal

/************************************************************************/            /*       42:  Binary Tree Postorder Traversal                               */            /************************************************************************/

LeetCode: Binary Tree Postorder Traversal 解题报告

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

[LeetCode][JavaScript]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 同样的配方,同样的味道.

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could y