Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree [1,null,2,3],

1
    \
     2
    /
   3

return [1,3,2].

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

法I: recursion

/**
* 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) {
inorder(root);
return ret;
} void inorder(TreeNode* root){
if(root==NULL) return; inorder(root->left);
ret.push_back(root->val);
inorder(root->right);
return; }
private:
vector<int> ret;
};

法II:iteration

/**
* 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) {
if(root==NULL) return ret; TreeNode* current = root;
stack<TreeNode*> s;
s.push(current);
while(){
while(current->left){
s.push(current->left); //push left child
current = current->left;
flag.insert(current);
} //After iterate left tree, visit root
while(!s.empty() && s.top()->right == NULL){
current = s.top();
s.pop(); //pop root
ret.push_back(current->val); //visit root
}
if(s.empty()) break; //terminate when stack is empty
current = s.top();
s.pop(); //pop root
ret.push_back(current->val); //visit root //go to right child
s.push(current->right); //push right child
current = current->right;
} return ret;
} private:
vector<int> ret;
};

94. Binary Tree Inorder Traversal(Tree, stack)的更多相关文章

  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. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

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

  5. 刷题94. Binary Tree Inorder Traversal

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

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

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

  7. LintCode Binary Tree Inorder Traversal

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

  8. 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 ...

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

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

随机推荐

  1. leetcode295

    public class MedianFinder { List<int> list = null; ; /** initialize your data structure here. ...

  2. [记录] Linux Apache隐藏index.php

    1. 在项目更目录下新建 .htaccess <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEng ...

  3. python 3.4 error: Microsoft Visual C++ 10.0 is required(Unable to find vcvarsall.bat)

    一些小技巧 我是在windows 64下安装的python3.4 Python 我在安装theano时报这个错,网上找了不少资料.自己摸索着解决了. 你先打开dos界面.我用set命令查看一下: 发现 ...

  4. 使用CLR Function代替T-SQL函数,优化检索效率

    前言: 在使用存储过程查询数据中,T-SQL字符串拆分函数效率低下,这个时候我们可以采用CLR Function代替T-SQL函数,使用DLL执行字符串分解过程,并返回值到SQL中.测试复杂运行的速度 ...

  5. mysql5.7.21免安装版配置步骤

    1. 下载mysql5.7.21 地址https://dev.mysql.com/downloads/mysql/ 2. 解压缩 任何文件夹都行,为了避免放在系统盘,我放到了E盘,目录为E:\Prog ...

  6. (笨方法)利用stat函数实现ls -l filename

    学习了一段时间的Linux了,但是我感觉我做不出来啥子,后头选择利用系统IO函数实现命令,先从ls走起吧.先来看看ls -l filename给我们显示了什么吧 : 可以看到,一共八项:文件类型.用户 ...

  7. Linux命令:zip

    语法: zip   [选项]   zip文件  源文件s   选项 全称 含义 举例 -r recursive 递归压缩子目录里的文件(包括子目录里的子目录) zip   -r    target.z ...

  8. Learn English like a Baby – How to Sound Native

    Learn English like a Baby – How to Sound Native Share Tweet Share Tagged With: tips & tricks Wha ...

  9. webpack 自动发现 entry 的配置和引用方式

    假定我们的项目目录为如下的样子: - root/ - assets/ - app/ - global.js - index/ - index.js - auth/ - login.js - regis ...

  10. adb INSTALL_FAILED_UPDATE_INCOMPATIBLE

    今天用Eclipse运行项目时出错: LOG: [2018-05-09 14:16:19 - Module_Android_Demo] ------------------------------ [ ...