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. c#调用WinRAR软件压缩和解压文件

    using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...

  2. nginx实现某个页面http访问,其余全部跳转到https

    全站https实现某个页面可以http访问,其余全部跳转到https,注意下面的location,如果不加root 配置 找不到这个文件的server { listen ; server_name w ...

  3. 定时执行线程池ScheduledExecutorService的使用

    ScheduledExecutorService progressExecutorService = Executors.newScheduledThreadPool(1); ScheduledFut ...

  4. Python基础之迭代器

    迭代器的优点: 1.可以使用for循环遍历: 2.可以节省内存空间: 3.可以有序的访问集合(set)数据结构内的元素. 迭代器的缺点: 只能向前,不能后退. 可迭代对象与不可迭代对象: 可迭代对象: ...

  5. JS-以鼠标位置为中心的滑轮放大功能demo1

    以鼠标位置为中心的滑轮放大功能demo1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  6. graph小案例

    (小案例,有五个人他们参见相亲节目,这个五个人分别是0,1,2,3,4,号选手,计算出追随者年龄大于被追随者年龄的人数和平均年龄) scala> import org.apache.spark. ...

  7. iOS iPhoneX/iPhoneXS/iPhoneXR/iPhoneXS Max系列适配

    以前异性屏只有一款iPhoneX,所以在适配的时候直接判断高度是否等于812即可判断是否是iPhoneX #define IS_IPHONE_X (IS_IPHONE && SCREE ...

  8. [译]GLUT教程 - 渲染到子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Rendering to Subwindows 先回顾一下之前 ...

  9. [译]GLUT教程 - 改变窗体大小

    Lighthouse3d.com >> GLUT Tutorial >> Basics >> Resizing the Window 上一章的例子创建了两个窗体,命 ...

  10. windows_64下python下载安装Numpy、Scipy、matplotlib模块

    本文应用的python3.6.3及其对应的Numpy.Scipy.matplotlib计算模块的cp36版本,其中Numpy是需要MKL版本的Numpy,这是后续安装Scipy的需要(本机系统win7 ...