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?

  

/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> res;
if(root == NULL) return res; stack<TreeNode *> mys;
while(! (mys.empty()) || root!= NULL)
{
while(root!= NULL){
mys.push(root);
root = root->left;
} root = mys.top();
mys.pop();
res.push_back(root->val);
root = root->right;
} return res;
}
};

  

LeetCode_Binary Tree Inorder Traversal的更多相关文章

  1. LintCode Binary Tree Inorder Traversal

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

  2. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. 3月3日(4) Binary Tree Inorder Traversal

    原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...

  4. 49. leetcode 94. Binary Tree Inorder Traversal

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

  5. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

    题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...

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

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

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

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

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

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

  9. 【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. 黑马程序员_JavaIO流(四)

    File概述 File类 用来将文件或者文件夹封装成对象. 方便对文件与文件夹的属性信息进行操作. File对象可以作为参数传递给流的构造函数. 了解File类中的常用方法. 字段:static St ...

  2. 2014-07-23 .NET实现微信公众号接入

    今天是在吾索实习的第11天.今天我跟我的实习小组的组员们,解决了关于使用ASP.NET进行微信公众号接入的问题.因为我们小组成员也是刚接触微信公众号的二次开发,所以在解决该问题的工程中也走了不少弯路. ...

  3. POJ_2488——骑士遍历棋盘,字典序走法

    Description Background The knight is getting bored of seeing the same black and white squares again ...

  4. [LeetCode] 203. Remove Linked List Elements 解题思路

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  5. Css背景

    index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  6. 为什么要使用Nginx?

    这里做了些基准测试表明nginx打败了其它的轻量级的web服务器和代理服务器,同样也赢了相对不是那么轻量级的产品. 有人说这些基准测试是不准确的,因为在这样那样的环境下,做的比较不一致.我倾向同意基准 ...

  7. Android Sensor Test

    魅蓝note可用 [{Sensor name="MPL Gyroscope", vendor="Invensense", version=1, type=4, ...

  8. LaTeX笔记

    1.上下标: $x^n$, $x^{123}$, $x_n$, $x_{123}$, $C_n^m$, $C_{100}^{50}$ $x^n$, $x^{123}$, $x_n$, $x_{123} ...

  9. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

  10. Android RatingBar自定义替换系统图片

    1.自定义替换提醒☆图片,准备两个图片添加到系统中去:如下:        在drewable下定义一个图片资源ratingbar_drawable.xml 1 2 3 4 5 6 7 8 9 10 ...