题目描述

给定一个二叉树,返回它的中序 遍历。

示例:

输入: [1,null,2,3]
1
\
2
/
3 输出: [1,3,2]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

解题思路

由于中序遍历的顺序是左孩子->父节点->右孩子,所以在遍历到某节点时,先依次把所有左孩子入栈,找到最左边的子节点后,输出该节点,然后继续遍历该节点的右孩子。

代码

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

LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)的更多相关文章

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

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

  2. LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

    题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...

  3. [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  5. Java实现 LeetCode 94 二叉树的中序遍历

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...

  6. Leetcode 94. 二叉树的中序遍历

    1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...

  7. leetcode 94二叉树的中序遍历

    递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  8. 【leetcode 94. 二叉树的中序遍历】解题报告

    前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...

  9. LeetCode 94 ——二叉树的中序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...

随机推荐

  1. chrome input 输入框去掉黄色

    -webkit-box-shadow: 0 0 0px 1000px white inset input设置内置阴影,要比你的input本身大

  2. Activity的跳转及返回值,activity的生命周期

    Activity生命周期 从创建到销毁的生命周期: onCreate()→onStart()→onResume()→onPouse()→onStop()→onDestroy() 从起动到后台再到前台: ...

  3. rank 和 ROW_NUMBER 区别

    SELECT * , RANK() OVER ( PARTITION BY APP_NAME ORDER BY SETTING_NAME,SETTING_CODE ASC ) AS Rank FROM ...

  4. 不能使用 float 和 double 来表示金额等精确的值

    不能使用 float 和 double 来表示金额等精确的值 关于面试,金额用什么数据类型? 不是 doube,更不是 float ,而是用 BigDecimal.对于金融项目,对于金额,误差是不能容 ...

  5. golang 系列学习(-) 数据类型

    数据类型的出现 在的编程语言中,数据类型用于声明函数和变量,数据类型的出现是为了要把数据分成数据所需要内存大小的不同数据,编程时需要什么样的内存就申请什么样的内存.就可以充分的利用内存,更好的霸控程序 ...

  6. SqlServer获取当前日期

    1. 获取当前日期 select GETDATE() 格式化: ) ---- :: 2. 获取当前年  --2017 3.获取当前月 --05或5 4.获取当前日期 --07或7 select DAY ...

  7. 初识LVS和LVS_NAT

    如果一台服务器承受过多的压力,那么服务可能会崩溃,所以,我们应该让一台服务器承受的压力在合理范围内,但是如果服务端必须要承受较大的压力,那么一台服务器可能无法满足我们的要求,所以我们可以使用多台服务器 ...

  8. NTP服务及时间同步

    环境: centos7 server   192.168.2.171 client    192.168.2.173.192.168.2.174 整体思路:173.174同步171的时间,171定时同 ...

  9. Error response from daemon: manifest for elasticsearch:latest not found

            五孔    35个     三孔空调     3个     一开五孔    10个     一开双控    10个     两开双控    2个     一开多控    3个     ...

  10. __slots__节约空间

    1.为什么要使用__slots__ Python 使用 dicts(hash table)缓存大量的静态资源(属性). 我们最近在Image类中,用仅仅一行__slots__代码,改变成使用tuple ...