【leetcode】Binary Tree Postorder Traversal (hard) ☆
二叉树的后序遍历
用标记右子树vector的方法
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans;
vector<TreeNode *> stack;
vector<bool> isRight;
stack.push_back(root);
isRight.push_back(false);
TreeNode * pNode = NULL;
while(!stack.empty())
{
while((pNode = stack.back()) != NULL)
{
pNode = pNode->left;
stack.push_back(pNode);
isRight.push_back(false);
}
while(isRight.back() == true)
{
stack.pop_back();
isRight.pop_back();
ans.push_back(stack.back()->val);
}
stack.pop_back();
isRight.pop_back();
if(!stack.empty())
{
pNode = stack.back();
stack.push_back(pNode->right);
isRight.push_back(true);
}
}
return ans;
}
仅用一个栈的方法https://oj.leetcode.com/discuss/14118/post-order-traversal-using-two-satcks
vector<int> postorderTraversal(TreeNode *root) {
stack<TreeNode *> st;
vector<int> vRet;
TreeNode *p, *pre = root;
if (!root) return vRet;
p = root->left;
st.push(root);
while (!st.empty() )
{
while (p) { st.push(p); p = p->left; }
p = st.top();
while (!p->right || pre==p->right)
{
vRet.push_back(p->val);
pre = p;
st.pop();
if (st.empty() ) break;
p = st.top();
}
p = p->right;
}
return vRet;
}
【leetcode】Binary Tree Postorder Traversal (hard) ☆的更多相关文章
- 【leetcode】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
- 【Leetcode】【hard】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 【Leetcode】Binary Tree Traversal
把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...
随机推荐
- [译]了解AngularJS $resource
原文: https://learnable.com/books/angularjs-novice-to-ninja/preview/understanding-angularjs-resource-e ...
- (免量产,免格式化)手动将PE安装到移动硬盘/U盘或无系统硬盘!
在一台没有装系统的电脑上,只要把XP系统启动文件,及引导菜单文件(ntldr,boot.ini,”bootfont.bin这个可有可无,主要作用是显示中文菜单”)和GRUB引导文件和PE系统文件复制到 ...
- ScriptManager与UpdatePanel总结
1.From http://www.cnblogs.com/Tim-Seven/archive/2011/02/11/1952409.html Ajax Extensions 2.ScriptMana ...
- 机器码call和jmp地址的计算
call和jmp都是跳转指令,但是call的同时会把pc地址压入堆栈,并且这两种方式都有远和近跳转.下面的分析不全,因为没有在网上找到足够的资料,个人创造这个情景还是有些困难. 1.例子中的call的 ...
- 文件操作 fopen() fclose()
#define _CRT_SECURE_NO_DEPRECATE /*取消scanf,printf不安全之类的错误提示*/ /* fopen example */ #include <stdio ...
- cocoapods pod install 安装报错 is not used in any concrete target
低版本的cocoa pods在编写Podfile文件时这样写就可以了 platform :ios, '8.0'pod 'AFNetworking' 高版本的cocoa pods在编写Podfile文件 ...
- 开关WIFI脚本
title wifi管理color A@echo on@echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@echo 1.启用并设定虚拟WiFi网卡;@echo 2.开启无线网络; ...
- 关于CSS的优先级,CSS优先级计算
原则一: 继承不如指定原则二: #id > .class > 标签选择符原则三:越具体越强大原则四:标签#id >#id ; 标签.class > .class CSS优先级权 ...
- MyEclipse 2013优化配置【转】
作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs MyEclipse 2013优化速度方案仍然主要有这么几个方面:去除无需加载的模块.取消冗余的配置.去除不 ...
- Redhat EL安装curses
1.下载curses安装包 http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz2. tar -zxvf nurses-5.6.tar.gz 3 ...