LeetCode 145. Binary Tree Postorder Traversal
来源:互联网 发布:电脑行业数据分析报告 编辑:程序博客网 时间:2024/05/17 03:28
利用递归,对于每个节点,如果没有左右子树那么就保存数值并返回,如果只有左子树,遍历左子树后保存数值并返回,如果只有右子树,遍历右子树后保存数值并返回,如果左右子树都有则遍历左子树,遍历右子树,保存数值后返回。
应该可以更加简单 ,明天再说吧。
int travel(vector<int>&v,TreeNode* root)
{
if(root->left==NULL&&root->right==NULL)
{
v.push_back(root->val);
return 0;
}
if(root->left!=NULL&&root->right==NULL)
{
travel(v,root->left);
v.push_back(root->val);
return 0;
}
if(root->left==NULL&&root->right!=NULL)
{
travel(v,root->right);
v.push_back(root->val);
return 0;
}
if(root->left!=NULL&&root->right!=NULL)
{
travel(v,root->left);
travel(v,root->right);
v.push_back(root->val);
return 0;
}
}
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>v;
travel(v,root);
return v;
}
};
- [LeetCode]145.Binary Tree Postorder Traversal
- [leetcode] 145.Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal LeetCode
- Leetcode 145. Binary Tree Postorder Traversal
- [LeetCode]145. Binary Tree Postorder Traversal
- Leetcode 145. Binary Tree Postorder Traversal
- leetcode 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode-145.Binary Tree Postorder Traversal
- [leetcode] 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- leetcode 145. Binary Tree Postorder Traversal
- [LeetCode]problem 145. Binary Tree Postorder Traversal
- Leetcode 145. Binary Tree Postorder Traversal
- [LeetCode] 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode:145. Binary Tree Postorder Traversal
- OSPF协议
- QSqlDatabase: QMYSQL driver not loaded problem under qt5.5 in ubuntu14
- UIApplication对象的生命周期事件及常用方法
- Linux网卡配置与绑定
- Codeforces 672E:Ultimate Weirdness of an Array 线段树
- LeetCode 145. Binary Tree Postorder Traversal
- DP 动态规划 Problem E 1005 最大递增子序列
- Not Fibonacci(矩阵连乘)
- ZOJ 3600 Taxi Fare (数学)
- iOS多线程
- LaTeX技巧001:ctex下使用其他中文字体
- 使用Jmeter进行http接口测试
- QT信号槽多次连接引起的错误修改
- PHP第三方登录—QQ登录