Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

解题:果然不能晚上做题,效率好低。看了讨论才学会的解法。设置一个指针next指向当前访问的节点,如果它不为空,就把它压栈,并且下一个访问它的左节点;如果它为空,就从栈顶一定是它的父节点,取出它的父节点,把这个父节点的值加入向量中,然后去访问父节点的右子。特别注意最后循环结束的条件是栈不空或者next指针不空,因为有可能栈里面的东西弹完了,next还指着一个节点,只有根节点的时候就是这种情况。

代码:

 /**
* 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> inorderTraversal(TreeNode *root) {
vector<int> ans;
stack<TreeNode*>s;
if(root == NULL)
return ans; TreeNode* next = root;
while(!s.empty()||next!=NULL){
if(next != NULL)
{
s.push(next);
next = next->left;
}
else{
next = s.top();
s.pop();
ans.push_back(next->val);
next = next->right; } }
return ans;
}
};

【leetcode刷题笔记】Binary Tree Inorder Traversal的更多相关文章

  1. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

  2. [刷题] 144 Binary Tree Preorder Traversal

    要求 二叉树的前序遍历 实现 递归 栈模拟          定义结构体 Command 模拟指令,字符串s描述命令,树节点node为指令作用的节点 定义栈 Stack 存储命令 1 #include ...

  3. LeetCode(94)Binary Tree Inorder Traversal

    题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def hel ...

  4. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  5. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  6. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

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

  7. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  8. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  9. LeetCode: Binary Tree Inorder Traversal 解题报告

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

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

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

随机推荐

  1. svn client命令

    经常使用svn命令说明 1.从SVN仓库中检索出代码到工作拷贝: # svn checkout https://svn.sinaapp.com/appname [workcopy] 当中workcop ...

  2. 自定义type='file'上传文件样式

    改变默认的上传文件样式: 用label作为替代 <input id="file_-1" type="file" name="file" ...

  3. wc 命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  4. Android备份和添加短信

    手机发送成功的,没有成功的,接受的短信都存放在手机自带的数据库中. 现在想要备份一下这个短信,需要访问这个数据库,然后查询所有的短信.就需要内容提供者(短信). 首先要找到内容提供者的uri.

  5. ios错误ignoring file xxx missing required architecture x86_64 in file

    错误ignoring file xxx missing required architecture x86_64 in file 解决方法: 1.在Project target里“Architectu ...

  6. a byte of vim -- 学习摘要

    说在前面的话 -- a byte of vim 是我见过的最介绍vim 最好的书,想了解强大的vim的人,或者是已经在使用vim而打算进一步了解的人,我感觉都应该看看这个,内容精炼但涵盖非常广,--& ...

  7. Cocos2d-x中使用第三方so库

    项目中假设使用到第三方的SDK,大多数是以.so动态共享库的文件打包给我们使用.怎样使用他们,见以下分析. 1.获得库文件 假如我们得到的库文件是libxxx.so(注:关于.so文件的命名方式,可百 ...

  8. SET IDENTITY_INSERT ON/OFF 权限

    今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...

  9. task10-14

    [说明]今天白天有事外出了,晚上会图书馆做了下面的任务,时间有点紧,好多没完成,明天要补上今天的! 一:今日完成 10.插入10条数据,查看有索引和无索引的情况下,Sql语句执行的效率 1)插入数据时 ...

  10. intellij idea pycharm phpstorm webstorm 使用 FiraCode 作为编程字体,更新后字符乱码问题解决

    先说使用下载 传送门 https://pan.baidu.com/s/1OI-novVYy-C74HIUfr9E6w windows: 1.下载后打开ttf文件夹,选择所有右键安装. 2.或者使用ch ...