【LeetCode题解】94_二叉树的中序遍历
【LeetCode题解】94_二叉树的中序遍历
@
描述
给定一个二叉树,返回它的中序遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
方法一:递归
Java 代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
inorderTraversal(root, ret);
return ret;
}
private void inorderTraversal(TreeNode root, List<Integer> ret) {
if (root == null) {
return;
}
inorderTraversal(root.left, ret);
ret.add(root.val);
inorderTraversal(root.right, ret);
}
}
复杂度分析:
- 时间复杂度:\(O(n)\),其中,\(n\) 为二叉树节点的数目
- 空间复杂度:平均为 \(O(log(n))\),最坏的情况为 \(O(n)\)
Python代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
def dfs(root, ret):
if root is None:
return
dfs(root.left, ret)
ret.append(root.val)
dfs(root.right, ret)
ret = list()
dfs(root, ret)
return ret
复杂度分析同上。
方法二:非递归
Java 代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
ret.add(cur.val);
cur = cur.right;
}
return ret;
}
}
复杂度分析:
- 时间复杂度:\(O(n)\),其中,\(n\) 为二叉树节点的数目
- 空间复杂度:\(O(h)\),其中,\(h\) 为二叉树的高度
Python 代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ret, stack = [], []
cur = root
while cur is not None or len(stack) > 0:
while cur is not None:
stack.append(cur)
cur = cur.left
cur = stack.pop()
ret.append(cur.val)
cur = cur.right
return ret
复杂度分析同上。
【LeetCode题解】94_二叉树的中序遍历的更多相关文章
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
- 【LeetCode】94. 二叉树的中序遍历
94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- LeetCode(94):二叉树的中序遍历
Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...
- [leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树
题目1:Construct Binary Tree from Preorder and Inorder Traversal 给定一棵二叉树的先序遍历和中序遍历,求重建二叉树. 思路: 1.先序遍历的第 ...
- Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
随机推荐
- TFS (Team Foundation Server) 2013集成Maven构建
Team Foundation Server原生就支持跨平台的构建,包括Ant和Maven两种构建方式.通过配置构建服务器,连接TFS源代码库,可以实现持续集成构建,自动检测代码库健康状况,进而实现自 ...
- C# 加载配置文件
//加载配置文件 var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .Add ...
- 数据库工具SQLite Expert Personal的简单使用
官方网站: sqliteexpert官方网址 - SQLite administration | SQLite Expert 先使用SQLite Expert Personal熟悉SQLite语句 插 ...
- WPF自定义ComboBox
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}"> &l ...
- 'Install app for SharePoint': Sideloading of apps is not enabled on this site
http://blog.lekman.com/2012/11/sharepoint-2013-sideloading-of-apps-is.html Solution: You need to ena ...
- 【OCP-12c】CUUG 071题库考试原题及答案解析(14)
14.(6-13) choose the best answer:View the Exhibit and examine the structure of the ORDERS table.Whic ...
- mysql 基础整合大全
mysql 数据库操作: 创建数据库: create database db_sanguo charset utf8; 切进db_sanguo use db_sanguo 删除数据库: drop d ...
- [Objective-C语言教程]命令行参数(23)
执行时,可以将一些值从命令行传递给Objective-C程序. 这些值称为命令行参数,很多时候它们对程序很重要,特别是当想要从外部控制程序而不是在代码中对这些值进行硬编码时就很有用了. 命令行参数使用 ...
- Django分页的实现
Django分页的实现 Django ORM 分页介绍 分页是网页浏览中常见到的一种形式,在数据量较大时,一个页面显示不全,采取分割数据由用户选择进行显示的方式. 基本实现 技术点 通过切片得到数据 ...
- CodeForces - 1025B Weakened Common Divisor
http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...