【LeetCode】Binary Tree Postorder Traversal
来源:互联网 发布:淘宝德国代购有假货吗 编辑:程序博客网 时间:2024/05/01 06:30
/** * 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*> s; vector<int> result; TreeNode *cur; //当前结点 TreeNode *pre=NULL; //前一次访问的结点 if(root) { s.push(root); } while(!s.empty()) { cur=s.top(); if((cur->left==NULL&&cur->right==NULL)|| (pre!=NULL&&(pre==cur->left||pre==cur->right))) { // cout<<cur->data; //如果当前结点没有孩子结点或者孩子节点都已被访问过 result.push_back(cur->val); s.pop(); pre=cur; } else { if(cur->right!=NULL) s.push(cur->right); if(cur->left!=NULL) s.push(cur->left); } } 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
- 解决ln -s 软链接产生Too many levels of symbolic links错误
- 正则基础
- bootloader
- 重联通分量
- ActiveReports 9 新功能:可视化查询设计器(VQD)介绍
- 【LeetCode】Binary Tree Postorder Traversal
- C++私有成员变量被对象直接访问
- UIView autoresizingMask
- 在MAC应用里显示多个图片(IKImageBrowserView控件使用)(二)
- java中的正则学习
- 实现Fragment数据的缓加载,即滑动到当前页再加载数据
- FFMPEG学习路线
- python27中mimetypes.py的UnicodeDecodeError
- 连通度(定点联通度&&边联通度)