LeetCode - Binary Tree Postorder Traversal
来源:互联网 发布:百度霸屏js跳转代码 编辑:程序博客网 时间:2024/05/29 18:12
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?
Recursive solution:
/** * 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; } if(root->left!=NULL){ vector<int>leftResult = postorderTraversal(root->left); result.insert(result.end(),leftResult.begin(),leftResult.end()); } if(root->right!=NULL){ vector<int> rightResult = postorderTraversal(root->right); result.insert(result.end(),rightResult.begin(),rightResult.end()); } result.push_back(root->val); return result; }};Iterative solution:
/** * 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<pair<TreeNode*,bool> > nodeStack; nodeStack.push(make_pair(root,false)); while(!nodeStack.empty()){ pair<TreeNode*,bool>& tmp=nodeStack.top(); if(tmp.second){ result.push_back(tmp.first->val); nodeStack.pop(); }else{ tmp.second = true; bool hasChild = false; if(tmp.first->right!=NULL){ nodeStack.push(make_pair(tmp.first->right,false)); hasChild = true; } if(tmp.first->left!=NULL){ nodeStack.push(make_pair(tmp.first->left,false)); hasChild = true; } if(!hasChild){ result.push_back(tmp.first->val); nodeStack.pop(); } } } return result; }};
0 0
- LeetCode Binary Tree Postorder Traversal
- LeetCode:Binary Tree Postorder Traversal
- Leetcode: Binary Tree Postorder Traversal
- Leetcode: Binary Tree Postorder Traversal
- LeetCode:Binary Tree Postorder Traversal
- Leetcode Binary Tree Postorder Traversal
- [LeetCode]Binary Tree Postorder Traversal
- [LeetCode] Binary Tree Postorder Traversal
- leetcode Binary Tree Postorder Traversal
- 【LeetCode】Binary Tree Postorder Traversal
- LeetCode | Binary Tree Postorder Traversal
- Leetcode: Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal - LeetCode
- LeetCode - Binary Tree Postorder Traversal
- [LeetCode]Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal -- LeetCode
- [LeetCode]Binary Tree Postorder Traversal
- 【LeetCode】Binary Tree Postorder Traversal
- 刷新界面:invalidate()和postInvalidate() 的区别及使用
- 怎样让Git忽略当前已经更改的文件
- 函数调用栈
- UVa688 Mobile Phone Coverage(扫描线法)
- Android 中设置只是程序第一次运行才显示的界面
- LeetCode - Binary Tree Postorder Traversal
- 【DP】 HDOJ 4960 Another OCD Patient
- shiro cas 单点登陆
- push,present,modal(模态)新界面的区别
- Asp.Net MVC 模型(使用LINQ to SQL创建Model类)
- wifi共享精灵2015 v3.0.438 官方版
- php 使用cxf的数据
- JavaSe基础XX22——正则表达式
- VC++检测VM、VPC虚拟机代码 .