题目描述:

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

示例:

输入: [1,null,2,3]
1
\
2
/
3 输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?

思路解析:

1)递归:没啥说的,左子树->根->右子树顺序去遍历

2)迭代计算:用栈,再用一个指针模拟访问过程

代码实现:

1)递归

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
//返回该二叉树的中序遍历
public static List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>();
dfsTraversal(res, root);
return res;
} private static void dfsTraversal(List<Integer> res, TreeNode root) {
if (root == null) {
return;
}
dfsTraversal(res, root.left);
res.add(root.val);
dfsTraversal(res, root.right);
}
}

2)迭代:

    public static List<Integer> inorderTraversal(TreeNode root) {

        //借助一个占来实现
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) {
return res;
}
TreeNode pNode = root;
while (!stack.isEmpty() || pNode != null) {
while (pNode != null) {
stack.push(pNode);
pNode = pNode.left;
}
//出栈,弹出元素加入结果集
TreeNode node = stack.pop();
res.add(node.val);
pNode = node.right;
}
return res;
}

时间复杂度:O(n)。递归函数 T(n)=2⋅T(n/2)+1。
空间复杂度:最坏情况下需要空间O(n)(每个节点只有一颗子树),平均情况为O(logn)(满二叉树结构排列)。

Leetcode题目94.二叉树的中序遍历(中等)的更多相关文章

  1. 【LeetCode】94. 二叉树的中序遍历

    94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...

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

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

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

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

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

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

  5. leetcode刷题-94二叉树的中序遍历

    题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...

  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. ant-design的Table组件自定义空状态

    Table,是antd中一个我们经常使用的组件,在官方文档中给出了非常详细的实例以及API, 但在我们在使用过程中可能需要根据项目的要求来定制空状态时的展示. 什么是空状态呢? 在antd的官方文档中 ...

  2. Oracle创建索引;查询索引

    1.创建索引 create index 索引名 on 表名(列名); 2.删除索引 drop index 索引名; 3.创建组合索引 create index 索引名 on 表名(列名1,,列名2); ...

  3. LEANGOO卡片

    转自:https://www.leangoo.com/leangoo_guide/leangoo_cards.html#toggle-id-10 Leangoo的卡片可以是需求.目标.任务.问题.缺陷 ...

  4. 7.使用EXPLAIN 来分析SQL和表结构_1

    explain:查看执行计划 使用explain 关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的 分析你的查询语句或是表结构的性能瓶颈 使用explain 可以获 ...

  5. websocket + TP5.1 + apache 配置步骤

    websocket + TP5.1 + apache 配置步骤 1. https ssl配置好 2. 检查php环境是否满足Workerman要求 curl -Ss http://www.worker ...

  6. 报错处理(UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 2: ill egal multibyte sequence)

    参照文[https://blog.csdn.net/Dillon2015/article/details/53204955]的说法, 第一个错 [UnicodeEncodeError:'gbk' co ...

  7. Hyperscan简介

    Hyperscan是一款来自于Intel的高性能的正则表达式匹配库. 参考 Hyperscan简介

  8. BZOJ3331 [BeiJing2013]压力[圆方树+树上差分]

    圆方树新技能get.具体笔记见图连通性问题学习笔记. 这题求无向图的必经点,这个是一个固定套路:首先,一张连通的无向图中,每对点双和点双之间是以一个且仅一个割点连接起来的(如果超过一个就不能是割点了) ...

  9. Python 字典 (4) 持续更新

    字典一种用名字来引用值的数据结构,这种数据结构称为 映射(mapping) .字典中的键可以是数字.字符串和元组. 字典 创建和使用 创建 phonebook = {'Aaron':133000000 ...

  10. wavefronts

    https://www.g-truc.net/post-0597.html https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in- ...