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. electron 项目的打包方式,以及 jquery 插件的修改使用

    < 一 > 应用打包 1,首先确定安装了 node 和 npm 2,全局安装打包依赖  => npm i electron-packager -g 3,打包命令 electron-p ...

  2. 白鹭引擎 - 碰撞检测 ( hitTestPoint )

    1, 矩形碰撞检测 class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须 ...

  3. ubuntu防火墙命令初探

    1.防火墙的状态与开关 1)$sudo ufw status  //查看防火墙的状态及当前的设置规则 2)$sudo ufw enable //开启防火墙 3)$sudo ufw disable // ...

  4. 人脸识别68个点<转>

    [Opencv] 于仕琪 人脸68个特征点分布情况 // 鼻尖 30 // 鼻根 27 // 下巴 8 // 左眼外角 36 // 左眼内角 39 // 右眼外角 45 // 右眼内角 42 // 嘴 ...

  5. APP-12-视觉技术-身份证识别

    1.Postman测试 图片转换为Base64:http://imgbase64.duoshitong.com/ Base64: Base64数据去掉表头文件:data:image/png;base6 ...

  6. lientDataset的Delta与XML相互转换

    一个ClientDataset的Delta与XML相互转换的文章:大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用De ...

  7. Servlet基本_サーブレットのライフサイクル、スレッドセーフ

    1.サーブレットのライフサイクル初期化時 ⇒ init() [初回リクエスト時] ↓リクエスト時 ⇒service() ⇒doGet() [Httpリクエストメソッドにより振り分け] 或は⇒doPos ...

  8. void类型详解

    void含义 void的字面意思是"无类型",void*则为"无类型指针",void*可以指向任何类型的数据. void几乎只有"注释"和限 ...

  9. Linux文件的时间

    关于Linux文件的ctime.atime和mtime等几个时间的介绍,推荐<Linux的3个文件时间>比较不错,这篇文章已经介绍的比较全面了,但是本文对它做进一步的解释,并对一些情况进行 ...

  10. C++17尝鲜:variant

    variant variant 是 C++17 所提供的变体类型.variant<X, Y, Z> 是可存放 X, Y, Z 这三种类型数据的变体类型. 与C语言中传统的 union 类型 ...