题目:

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

Given a binary tree, return the inorder traversal of its nodes' values.

示例:

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

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

Follow up: Recursive solution is trivial, could you do it iteratively?

解题思路:

百度百科:二叉树的中序遍历:https://baike.baidu.com/item/中序遍历

遍历顺序:左子节点 --> 根节点 --> 右子节点

如下所示的二叉树:

       A
/ \
B C
/ \ / \
D E F G

其遍历顺序为:D -> B -> E -> A ->F -> C -> G

二叉树遍历可以用 DFS(深度优先搜索)完成,其实现方式为:递归或借助数据结构 栈 迭代。

这种遍历方式本质上是一个先进后出的栈式遍历方式,递归方法实际也是用递归方式实现栈的先进后出。

DFS-递归:

Java:

class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();//数组
dfs_recursion(root, list);//传入递归函数
return list;
} private void dfs_recursion(TreeNode node, List<Integer> list) {
if (node == null) return;//基线条件
dfs_recursion(node.left, list);//先遍历左子节点
list.add(node.val);//遍历到左子节点的顶点,取出该节点的值
dfs_recursion(node.right, list);//递归遍历右节点
}
}

Python3:

class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
#数组
res = list()
#传入递归函数
self.dfs_recursion(root, res)
return res def dfs_recursion(self, node: TreeNode, res: List[int]):
#基线条件
if not node: return
#递归遍历左子节点
self.dfs_recursion(node.left, res)
#取顶点节点的值
res.append(node.val)
#递归遍历右子节点
self.dfs_recursion(node.right, res)

DFS-迭代:

Java:

class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();//数组
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();//用数据结构栈暂存节点
TreeNode cur = root;//定义当前节点
while (!stack.isEmpty() || cur != null) {//循环条件:栈不空或当前节点不空
if (cur != null) {//当前节点不空时
stack.push(cur);//当前节点入栈
cur = cur.left;//刷新当前节点
} else {//当前节点空时
cur = stack.pop();//当前节点的父节点出栈
list.add(cur.val);//数组存入节点的值
cur = cur.right;刷新当前节点
}
}
return list;
}
}

Python:

class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
#初始化数组、栈
res, stack = list(), list()
#当前节点指向根节点
cur = root
#递归条件为:栈或当前节点非空
while stack or cur:
if cur:
#当前节点非空时入栈
stack.append(cur)
#刷新当前节点
cur = cur.left
else:
#当前节点为空时其父节点出栈
cur = stack.pop()
#其值存入数组
res.append(cur.val)
#刷新当前节点
cur =cur.right
return res

一起学习吖,欢迎关注:爱写Bug

LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal的更多相关文章

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

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

  2. [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  3. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

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

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

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

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

  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. app自动化测试环境搭建之node+appium+ADT+MUMU模拟器

    一.安装Microsoft .NET Framework 4.5 检测本机已安装的程序中,是否已经安装Microsoft .NET Framework 4.5及以上的版本 如果没有安装,则获取安装文件 ...

  2. LINUX CFS 调度tick逻辑,即check_preemt_tick解析

    计算当前task在这个tick周期实际用时delta_exetime, 更新当前task的vruntime; 根据权重,重新计算调度period,计算当前task的应得时间片slice(idle_ru ...

  3. C#排序案例

    using System; namespace 排序案例 { class Program { static void Main(string[] args) { //定义随机数列 int a, b, ...

  4. C# download big file

    I had validated this.To download SSMS which is more than 500M+ static void Main(string[] args) { str ...

  5. 函数截流---js

    <div id="show">0</div> <button id="btn">click</button> & ...

  6. Android Studio当中的创建新方法的快捷键该如何使用?

    当有红线出现的时候,我们的代码并没有编译出错,则需要输入alt+enter则可以得到相应的神奇效果了.这个方法我竟然今天才知道,也真是丢脸了.比如说我们书写了一个新的没有创建的方法,我们直接输入alt ...

  7. react.js父子组件通信

    这里通过todolist的功能来说明 父组件: import React,{ Component,Fragment } from 'react'; import TodoItem from './To ...

  8. C语言中变量和函数的作用域和链接属性

    C语言中变量和函数的作用域和链接属性 作用域 代码块作用域: 代码块指的是使用"{}"包围起来的部分. 在代码块中定义的变量,代码块之外是不能访问的. 代码块嵌套之后的变量作用域, ...

  9. python 指定字符串位置查找

    指定字符串位置查找 #指定字符查找 s = 'F:/my_pycharm/pycharm_project/CSV表格/10.csv' print(s.find('/')) # 2, 第一个/在2位置 ...

  10. mongoDB的安全相关

    开启认证: 在配置文件里新增一行 auth = true   创建用户: 1.创建语法:createUser 2.{user:"<name>", pwd:"& ...