LeetCode OJ 之 Binary Tree Postorder Traversal (二叉树的后序遍历)
来源:互联网 发布:ubuntu安装优麒麟市场 编辑:程序博客网 时间:2024/06/06 02:16
题目:
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?
思路:
见代码。
递归版代码:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> postorderTraversal(TreeNode *root) { vector<int> result; if(root == NULL) return result; postorderTraversal(root,result); return result; } void postorderTraversal(TreeNode *root , vector<int> &result) { if(root == NULL) return ; postorderTraversal(root->left,result); postorderTraversal(root->right,result); result.push_back(root->val); }};
非递归版代码1:
思路:前序遍历的顺序是:root-left-right,用栈实现就是 root入栈,root出栈,输出root ,right入栈,left入栈(),前序遍历详情参看:http://blog.csdn.net/u012243115/article/details/42000877 其中的非递归版本代码2。
后序遍历的顺序是:left-right-root. ,可以先遍历 root-right-left ,然后再对结果reverse一下,即是后序遍历的结果。
对于root-right-left 的遍历方法类似前序遍历。用栈实现就是 root入栈,root出栈,输出root,left入栈,right入栈。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> postorderTraversal(TreeNode *root) { vector<int> result; if(root == NULL) return result; stack<TreeNode *> stk; TreeNode *p = root; stk.push(p); while(!stk.empty()) { p = stk.top(); stk.pop(); result.push_back(p->val); if(p->left != NULL) stk.push(p->left); if(p->right != NULL) stk.push(p->right); } reverse(result.begin(),result.end()); return result; }};
非递归版代码2:
详情请参看:http://blog.csdn.net/u012243115/article/details/40615603
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> postorderTraversal(TreeNode *root) { vector<int> result; if(root == NULL) return result; TreeNode *p = root; stack<pair<TreeNode *,bool> > stk; while(p != NULL || !stk.empty()) { if(p) { stk.push(make_pair(p , false)); p = p->left; } else if(stk.top().second == false) { stk.top().second = true; p = stk.top().first->right; } else { result.push_back(stk.top().first->val); stk.pop(); } } return result; }};
非递归版代码3:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> postorderTraversal(TreeNode *root) { vector<int> result; stack<TreeNode *> s; TreeNode *p = root , *pre = NULL; while(p || !s.empty()) { if(p) { s.push(p); p = p->left; //走到最左边 } else { p = s.top(); //取得栈顶 //如果右孩子非空,且右孩子未遍历过,则遍历右字树 if(p->right && p->right != pre) { p = p->right; s.push(p); p = p->left; } //如果右孩子为空,或者p->right == pre即上次遍历的结点pre为右孩子才遍历当前结点(即右孩子遍历过才遍历根结点) else { s.pop(); result.push_back(p->val); pre = p; p = NULL; //这一步很重要 } } } return result; }};
0 0
- LeetCode OJ 之 Binary Tree Postorder Traversal (二叉树的后序遍历)
- Binary Tree Postorder Traversal-二叉树的后序遍历
- Binary Tree Postorder Traversal 二叉树的后序遍历
- 二叉树的后序遍历 Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal 二叉树的后序遍历
- binary-tree-postorder-traversal(后序遍历二叉树)
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
- [LeetCode]145. Binary Tree Postorder Traversal--二叉树的后序遍历
- 【LeetCode】Binary Tree Postorder Traversal 二叉树的后序遍历
- 【leetcode】145Binary Tree Postorder Traversal(二叉树非递归后序遍历)
- LeetCode OJ 之 Construct Binary Tree from Inorder and Postorder Traversal (由二叉树的中序和后序序列构造二叉树)
- Binary Tree Postorder Traversal 二叉树的后序遍历(迭代非递归版本)
- 68.Binary Tree Postorder Traversal-二叉树的后序遍历(容易题)
- LeetCode OJ 之 Binary Tree Inorder Traversal (二叉树的中序遍历)
- LeetCode OJ 之 Binary Tree Preorder Traversal (二叉树的前序遍历)
- LeetCode 145:Binary Tree Postorder Traversal(后序遍历)
- LeetCode 145 Binary Tree Postorder Traversal (后序遍历二叉树)
- android 4.4 高通平台开发笔记
- TOP域名细数数字域名的魅力
- sqoop1.99.4的安装和使用
- 老域名建站没有想象的那么美好
- UIButton的以下开发者不长用的方法。。。。。。。。。
- LeetCode OJ 之 Binary Tree Postorder Traversal (二叉树的后序遍历)
- Null value was assigned to a property of primitive type setter of"原因及解决方
- MongoDB运行状态、性能监控,分析
- python的pip和virtualenv使用心得
- 动态代理
- SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)
- IE下限制输入法切换的css
- 管理员以标准权限运行时
- Oracle学习笔记