问题描述

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. windows游戏编程地址

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22309325 作者:jadeshu   邮箱: jades ...

  2. redis之不重启,切换RDB备份到AOF备份

    确保redis版本在2.2以上 [root@pyyuc /data 22:23:30]#redis-server -v Redis server v=4.0.10 sha=00000000:0 mal ...

  3. B. Uniqueness(尺取)

    B. Uniqueness time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. @Transactional注解详细用法

    概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型, ...

  5. JAVA基础知识|Socket

    一.什么是Socket? Socket本身并不是协议,是一套完成TCP.UDP协议的调用接口(API),通过socket我们才能使用TCP/IP协议(JAVA基础知识|TCP/IP协议).Socket ...

  6. Ubuntu下Nginx的安装和卸载

    环境是Ubuntu 16.04 安装: sudo apt-get update sudo apt-get install nginx 卸载: sudo apt-get --purge remove n ...

  7. fatal: unable to access 'https://github.com/Homebrew/homebrew-core/'

    LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54 安装curl "https://nodejs.org/dist/latest/node-${VE ...

  8. Mysql 清空数据后,释放硬盘文件

    删除表数据后执行:optimize table [表名]

  9. Linux学习:使用 procrank 测量系统内存使用情况

    程序员应该了解一个基本问题:我的程序使用了多少内存?这可能是一个简单的问题,但是对于像Linux这样的虚拟内存操作系统,答案是相当复杂的,因为top和ps给出的数字不能简单相加.进程中两个最常见的内存 ...

  10. PCA人脸识别学习笔记---代码篇

    查看并改变当前的工作路径 path="D:\\python-file\\faker" os.chdir(path) retval=os.getcwd() print(retval) ...