问题描述

Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右)

Example:

Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]

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

参考答案

 /**
* Definition for a binary tree node.
* 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) {
// init
vector<int> res;
stack<TreeNode* > st;
TreeNode* p = root; // 初始化根节点 while(p||!st.empty()){
// 一旦遇到节点,先考虑左边的,直到尽头,如果没有之后的右node,就停止运行了
while(p){
st.push(p);
p = p->left;
}
// 立刻提取p的信息,并且把p弹出来。如果进入while,那么这一步只会是左孩子,如果没进入while,那么会是父节点/右节点。
p = st.top();
st.pop();
res.push_back(p->val);
// 把p 变成p的右孩子
p = p->right;
}
return res;
}
};

LC 94. Binary Tree Inorder Traversal的更多相关文章

  1. 49. leetcode 94. Binary Tree Inorder Traversal

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

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

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

  3. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  4. 刷题94. Binary Tree Inorder Traversal

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

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

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

  6. Leetcode 94. Binary Tree Inorder Traversal

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

  7. 94. Binary Tree Inorder Traversal

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

  8. leetcode 94 Binary Tree Inorder Traversal ----- java

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

  9. Java [Leetcode 94]Binary Tree Inorder Traversal

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

随机推荐

  1. javascript监听浏览器前进后退

    window.addEventListener("popstate", function () { backStatus=true; return; })

  2. 集合家族——stack

    一.概述 在 Java 中 Stack 类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构,它采用典型的先进后出的操作方式完成的 它通过五个操作对类 Vector 进行了扩展 ,允许将向 ...

  3. springboot+druid+sqlite遇到的问题

    1.springboot中使用druid查询sqlite报错getFetchDirection error ResultSet closed https://blog.csdn.net/u011943 ...

  4. tomcat单机多实例

    catalina.home指向公用信息的位置,就是bin和lib的父目录. catalina.base指向每个Tomcat目录私有信息的位置,就是conf.logs.temp.webapps和work ...

  5. 使用vagrant一键部署本地php开发环境(一)

    一:我们为什么需要用这玩意 我们在开发中经常会面临的问题:环境不一致,有人用Mac有人用Windos还有几个用linux的,而我们的服务器都是linux.    在我本地是可以的啊,我测了都,没有问题 ...

  6. 循环引擎 greenlet 没有显式调度的微线程,换言之 协程

    小结: 1. micro-thread with no implicit scheduling; coroutines, in other words. 没有显式调度的微线程,换言之 协程 2. 一个 ...

  7. 【转】netty4.1.32 pipeline的添加顺序和执行顺序

    原文:https://www.cnblogs.com/ruber/p/10186571.html 本文只想讨论一下pipeline的执行顺序问题,因为这个搞不明白就不知道先添加编码还是解码,是不是可以 ...

  8. OpenCV3.4.1快速集成到Android studio中,10分钟搞定

    OpenCV3.4.1快速集成到Android studio中,10分钟搞定     转载 https://blog.csdn.net/yu540135101/article/details/8259 ...

  9. XM概述

    概述: Extensible Markup Language: 可扩展的标记语言 特点: 语法很严格 标签自定义 作用: * 存储数据 * 做配置文件 * 用于进行数据传输 文档声明: 标示这个文档是 ...

  10. Object.keys() 遍历对象

    Object.keys()方法的运用与数组遍历 Object.keys()用于获得由对象属性名组成的数组,可与数组遍历相结合使用,用起来效果杠杠滴.数组遍历可以用for()或forEach()来实现, ...