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

示例:

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

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

递归:

class Solution {
public:
vector<int> res;
vector<int> inorderTraversal(TreeNode* root)
{
if(root == NULL)
return res;
if(root ->left != NULL)
{
inorderTraversal(root ->left);
}
res.push_back(root ->val);
if(root ->right != NULL)
{
inorderTraversal(root ->right);
}
return res;
}
};

迭代:

class Solution {
public:
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> res;
stack<TreeNode*> s;
TreeNode *cur = root;
while(!s.empty() || cur != NULL)
{
if(cur != NULL)
{
s.push(cur);
cur = cur ->left;
}
else
{
cur = s.top();
s.pop();
res.push_back(cur ->val);
cur = cur ->right;
}
}
return res;
}
};

Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)的更多相关文章

  1. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

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

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

  3. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  4. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  5. [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆

    二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...

  6. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  7. Binary Tree Inorder Traversal-非递归实现中序遍历二叉树

    题目描述: 给定一颗二叉树,使用非递归方法实现二叉树的中序遍历 题目来源: http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ ...

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  9. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

随机推荐

  1. selenium基础(警告框的处理)

    selenium基础(警告框的处理) 在webdriver中处理JavaScript所产生的的警告框有三种类型 alert confirm prompt 划转到警告框的方法是:driver.switc ...

  2. sparkStreaming结合sparkSql进行日志分析

    package testimport java.util.Propertiesimport org.apache.spark.SparkConfimport org.apache.spark.Spar ...

  3. uoj60 怎样提高智商

    题意:你需要构造n个四项选择题.格式为:问在前i个问题中选了几个hi字母? 输出有最多正确答案的构造方案. 标程: #include<cstdio> using namespace std ...

  4. 学习笔记css3

    边框 盒子圆角 border-radius:5px / 20%: border-radius:5px 4px 3px 2px; 左上,右上,右下,左下 盒子阴影 box-shadow:box-shad ...

  5. java分析工具arthas

    wget https://alibaba.github.io/arthas/arthas-boot.jar java -jar arthas-boot.jar --target-ip 0.0.0.0

  6. 《DSP using MATLAB》Problem 8.2

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. 04-python 学习第四点-装饰器

    装饰器就是一个函数,他是通过不修改某个函数的源代码和调用方式的前提下可以添加新功能的一种函数.在python 中装饰器一般采用高阶函数和嵌套函数达到装饰的作用,下面进行实例讲解: 1.目前有一个网址有 ...

  8. DEV 皮肤的使用

    一.皮肤的使用 拖入defaultLookAndFeel 组件到窗体中 拖入ribbonControl 控件到窗体中 将窗体继承为 DevExpress.XtraBars.Ribbon.RibbonF ...

  9. 08-background详解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. PKU 百练OJ Arbitrage

    http://bailian.openjudge.cn/practice/2240/ #include <iostream> #include <string> #includ ...