leetCode 145:Binary Tree Postorder Traversal
来源:互联网 发布:linux系统解压缩文件 编辑:程序博客网 时间:2024/04/30 21:51
//非递归的后序遍历二叉树:
//首先跳转到某个节点的最左儿子并把路径上经过的点压栈,如果最左儿子没有右儿子或者其右儿子已经被访问过了,那么我们遍历它。
/*重点在于当访问栈顶元素时,要看栈顶元素的右子树是否被访问过了,这就需要先把栈顶元素POP出来,判断是否可以遍历,如果不行,需要再把它压入栈中,并且指向其右子树*/
vector<int> postorderTraversal(TreeNode* root){ vector<int>res; TreeNode*p=root,*q=root; stack<TreeNode*>ms; while(p){ for(;p->left;p=p->left) ms.push(p); while(p->right==0||p->right==q){//判断是否可以访问它 res.push_back(p->val); q=p; if(ms.empty()) return; p=ms.top();//栈顶元素拿出来继续判断 ms.pop(); } ms.push(p);//把它重新压栈 p=p->right;//访问它的右子树 } return res;}
0 0
- LeetCode(145)Binary Tree Postorder Traversal
- LeetCode: Binary Tree Postorder Traversal [145]
- LeetCode 145 Binary Tree Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal
- leetcode || 145、Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal - LeetCode 145
- 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, Hard] Binary Tree Postorder Traversal
- leetcode[145]:Binary Tree Postorder Traversal
- [leetcode-145]Binary Tree Postorder Traversal(c++)
- 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
- NSOperation创建的三种方式
- ImageLoader加载本地图片的实现————防OOM
- NSOperationQueue返回主线程
- 选择恐惧症的福音!教你认清MVC,MVP和MVVM
- 用VS2010编译C++项目时出现这样的错误:
- leetCode 145:Binary Tree Postorder Traversal
- iOS _MJRefresh 导入最新Xcode 报错 解决方法
- Url转图片
- php简单的分页原理
- CoreGraphics CGPath(路径)
- 每天laravel-20160717|Command-1
- stringByAppendingPathExtension拼接路径
- Centos 6.5 编译安装Nginx+php+Mysql
- 获取路径的最后一个文件名lastPathComponent