递归实现当然太简单,也用不着为了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的更多相关文章

  1. leetcode第一刷_Binary Tree Zigzag Level Order Traversal

    以出现的频率来看.树的层序遍历一定是考察的重点,除非工作人员想找题水数量. zigzag,还是有几道题的,层序的这个非常easy,假设是奇数层.reverse下面就可以.无他.我写的时候预计还不知道这 ...

  2. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  3. 【一天一道LeetCode】#94. Binary Tree Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  5. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  6. 【LeetCode】94. Binary Tree Inorder Traversal

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  7. LeetCode OJ 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  9. [leetcode]_Binary Tree Inorder Traversal

    题目:二叉树的中序遍历. 思路:用递归来写中序遍历非常简单.但是题目直接挑衅说,----->"Recursive solution is trivial".好吧.谁怕谁小狗. ...

随机推荐

  1. ActionBarSherlock的使用--------(一)配置

    ActionBarSherlock的使用--(一)配置 简介: 从android 3.0开始,android加入了一个新的api,actoinbar,随着android 4.0的发布和慢慢的推广开来, ...

  2. WeasyPrint - Converts HTML + CSS to PDF - WeasyPrint converts HTML/CSS documents to PDF

    WeasyPrint - Converts HTML + CSS to PDF - WeasyPrint converts HTML/CSS documents to PDF WeasyPrint c ...

  3. GCD自己做的一些简单总结

    GCD总结 GCD  Grand Central Dispatch  牛逼的中枢调度器 GCD中各种队列的执行效果 想看线程  必须是异步函数  并且不是主队列 注意:使用sync函数往当前串行队列添 ...

  4. 彻底解决:Keil编译提示“File has been changed outside the editor, reload?”提示!

    如图所示,很多同学在使用keil时都可能会碰到上图中的“File has been changed outside the editor, reload?”提示,很令人烦心.当遇到此提示,首先不要郁闷 ...

  5. UC/0S2之基础总结

    堆栈,就是在存储器中按数据“后进先出(LIFO)[类比杯子]”的原则组织的连续存储空间,为了满足任务切换和响应中断保存CPU寄存器中的内容及存储任务私有数据的需要,每个任务都应该配有自己的堆栈, 注意 ...

  6. ;(function($,window,undefined){})(jQuery,window)详细解析————借助B5教程解析自己整理了一下

    在jquery插件中我们经常看到以下这段代码 ;(function ( $, window, document, undefined ){ //函数体内具体代码 })(jQuery, window,d ...

  7. CBV进阶(一)

    http_method_names , http_method_not_allowd 用途:views只接受这些请求. 范围View Default:['get', 'post', 'put', 'p ...

  8. Eclipse中DTD验证导致无法进行代码提示的问题(转)

    eclipse的xml插件进行dtd验证的时候由于找不到dtd或请求主机相应超时,会出现黄色的警告 The file cannot be validated as the host "www ...

  9. 用macports装了一份openssl

    我已经用macports装了一份openssl,然后自己又编译了一份openssl....第三方给Mac出的一个类似BSD Ports的一个软件包管理工具装的话只需要sudo port install ...

  10. python urllib和urllib2 区别

    python有一个基础的库叫httplib.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现 ...