leetcode第一刷_Binary Tree Inorder Traversal
递归实现当然太简单,也用不着为了ac走这样的捷径吧。。非递归实现还挺有意思的。
树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来。对于中序遍历,先要訪问最左下的节点,一定是进入循环后,不断的往左下走,走到不能走为止,这时候,能够从栈中弹出訪问的节点,相当于“左根右”过程的“根”,然后应该怎么做呢?想一下中序遍历完根节点之后应该干嘛,对,是走到右子树中继续反复这个过程,可是有一点,假设这个节点不包括右子树怎么办?这样的情况下,下一个应该訪问的节点应该是他的父亲,他的父亲如今应该就在栈顶,可是不要忘记每次进入循环的时候都是一直往左子树跑,所以要把当前节点置空,这样跳过走左子树的循环直接操作栈。
是不是比仅仅写递归的收获多一些?
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> res;
if(root == NULL) return res;
stack<TreeNode *> sta;
TreeNode *pNode = root;
sta.push(root);
while(!sta.empty()){
while(pNode&&pNode->left){
sta.push(pNode->left);
pNode = pNode->left;
}
pNode = sta.top();
sta.pop();
res.push_back(pNode->val);
if(pNode->right){
sta.push(pNode->right);
pNode = pNode->right;
}else
pNode = NULL;
}
return res;
}
};
leetcode第一刷_Binary Tree Inorder Traversal的更多相关文章
- leetcode第一刷_Binary Tree Zigzag Level Order Traversal
以出现的频率来看.树的层序遍历一定是考察的重点,除非工作人员想找题水数量. zigzag,还是有几道题的,层序的这个非常easy,假设是奇数层.reverse下面就可以.无他.我写的时候预计还不知道这 ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 【一天一道LeetCode】#94. Binary Tree Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- 【LeetCode】94. Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- LeetCode OJ 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [leetcode]_Binary Tree Inorder Traversal
题目:二叉树的中序遍历. 思路:用递归来写中序遍历非常简单.但是题目直接挑衅说,----->"Recursive solution is trivial".好吧.谁怕谁小狗. ...
随机推荐
- 基于Visual C++2013拆解世界五百强面试题--题13-找最大公共子字符串
编程实现:找出两个字符串中最大公共子字符串,如"abccade"和"dgcadde"的最大子字符串为"cad". 如果不考虑效率的话直接比较 ...
- HDU 1720 A+B Coming
#include <string> #include <cstdio> #include <iostream> using namespace std; int c ...
- 关于GROUP BY的应用
前面收藏了别人的SQL语句操作,可是没有实战,也未知学的如何 正好今天有个事需要做一下 (sql server 2000) 三个表:stuInf,sType,sinInf分别为学生信息表,类型表,信息 ...
- Memcached源代码分析 - Memcached源代码分析之消息回应(3)
文章列表: <Memcached源代码分析 - Memcached源代码分析之基于Libevent的网络模型(1)> <Memcached源代码分析 - Memcached源代码分析 ...
- 自己动手写Java大整数《3》除法和十进制转换
之前已经完毕了大整数的表示.绝对值的比較大小.取负值.加减法运算以及乘法运算. 详细见前两篇博客(自己动手写Java * ). 这里加入除法运算. 另外看到作者Pauls Gedanken在blog( ...
- 大一C语言结课设计之《简单计算器》
/*===============================================*\ ** 设计目的:简单计算器,计算形如10*(20.2-30.6)+5.0/2的表达式值 ** 简 ...
- 基于表单的身份验证(FBA)
https://technet.microsoft.com/zh-cn/library/ee806890(office.15).aspx http://www.tuicool.com/articles ...
- mysql 中的 IF 和 IFNULL 用法
IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境. IF(S ...
- vim: 字符串替换
:s/str1/str2/ 替换当前行第一个 str1 为 str2:s/str1/str2/g 替换当前行所有 str1 为 str2 ( 注意, s/xx/xxx/g 语句从s开始,中间的空格视 ...
- linux ln 命令(转载)
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...