leetcode Binary Tree Postorder Traversal
来源:互联网 发布:七天网络学生空间登录 编辑:程序博客网 时间:2024/06/06 19:56
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) { stack<TreeNode *> tree; tree.push(root); TreeNode *q=NULL; vector<int> a; if(root==NULL) return a; while(!tree.empty()) { TreeNode *p=tree.top(); if((p->left==NULL&&p->right==NULL)||(q!=NULL&&(q==p->left||q==p->right))) { a.push_back(p->val); q=p; tree.pop(); } else { if(p->right!=NULL) { tree.push(p->right); q=p->right; } if(p->left!=NULL) { tree.push(p->left); q=p->left; } } } return a; }};
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
- Sessions, Desktops and Windows Stations
- Java中的类,成员变量的值,在声明的时候直接赋值与在构造函数中再赋值,这个是无所谓的,还是有什么规矩?
- Objective-C 基本语法:实例变量与成员变量的区别
- IOS 点击空白处隐藏键盘的几种方法
- Call requires API level 16 (current min is 14): android.view.View#announceForAccessibility问题解决
- leetcode Binary Tree Postorder Traversal
- 堆排序
- dsfgdsfgdsfg
- PlayFramework1.2.4 一些使用中的记录2(多文件上传)
- 动态库中使用对话框资源
- Java读取properties文件的六种方法
- Play框架启动报错 UnexpectedException: While applying class play.classloading...
- 是的分跟得上风格的发生过
- php post发送数据包以及解析post数据包