题目描述:

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

示例:

输入: [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. IOS 改变UISearchBar的背景色

    之前网上提供的方法试了很多种  都不能很好的去掉背景色  ,修改背景色方法如下: searchbar.barStyle = UIBarStyleBlackTranslucent; searchbar. ...

  2. 判断两个list是否元素一样

    首先创建枚举 public enum TheType { type1 = , type2 = , type3 = } 1.如果不考虑顺序,即顺序不一样,只要元素都一样即可 List<TheTyp ...

  3. Zookeeper 入门详解

    zookeeper zookeeper是什么 Apache ZooKeeper是Apache软件基金会的一个软件项目,他为大型分布式计算提供开源的分布式配置服务.同步服务和命名注册.ZooKeeper ...

  4. idea内存溢出解决方法

    在Run/Debug configuration 的vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=256m eclipse: -Xm ...

  5. asp.net网站部署在云服务器windows server 2008上

    搭建一个网站需要以下4个准备: 1.域名解析 2.(云)服务器 3.数据库 4.网站代码 其中1可以可以去DNSPOD申请,同时需要进行备案,在上面就都可以完成.2用的是阿里云服务器windows s ...

  6. Navicat连接腾讯云实例MySQL

    Navicat连接腾讯云实例MySQL 授权所有的用户通过root账户 root密码登陆远程数据库 连接腾讯云实例上的MySQL数据库 这里的密码填入数据库的密码 这里的密码填入登陆云实例的密码也就是 ...

  7. web开发:Bootstrap

    一.ajax请求 二.前台服务器概念 三.bs导读 四.bs引入 五.bs容器与响应式 一.ajax请求 - 后台 ```python# 通过flask框架搭建后台from flask import ...

  8. Nexus Repository Manager OSS 2 配置阿里云私服做代理的坑

    安装 搭建 Nexus 私服很简单,官网下载,解压: 使用管理员权限打开cmd: > cd nexus---bundle\nexus--\bin > nexus.bat install # ...

  9. Linux搭建.net core CI/CD环境

    一.简介 微服务开发中自动化.持续化工程十分重要,在成熟的CI/CD环境中项目团队可以灵活分配,大大提供团队效率.如果还不了解什么是CI/CD,可以先查看相关文章,这里主要介绍环境的搭建,相关原理就不 ...

  10. P5496 【模板】回文自动机(PAM)

    做一下强制在线处理即可 #include <cstdio> #include <algorithm> #include <cstring> using namesp ...