leecode刷题(29)-- 二叉树的中序遍历

二叉树的中序遍历

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

示例:

输入: [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 {
List<Integer> list = new ArrayList();
public List<Integer> inorderTraversal(TreeNode root) {
if (root != null) {
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
return list;
}
}

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: TreeNode) -> List[int]:
res = []
if root is not None:
res = res + self.inorderTraversal(root.left)
res = res + [root.val]
res = res + self.inorderTraversal(root.right)
return res

总结

对比如下:

leecode刷题(29)-- 二叉树的中序遍历的更多相关文章

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

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

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

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

  3. 数据结构《10》----二叉树 Morris 中序遍历

    无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...

  4. lintcode:二叉树的中序遍历

    题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...

  5. LeetCode(94):二叉树的中序遍历

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

  6. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  7. LintCode-67.二叉树的中序遍历

    二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...

  8. LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

    题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...

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

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

随机推荐

  1. JVM 监控工具——jstatd

    1. 简介 jstatd是一个rmi的server应用,用于监控jvm的创建和结束,并且提供接口让监控工具(如visualvm)可以远程连接到本机的jvms . 注意是jvms,就是说运行jstatd ...

  2. dpkg -l 命令返回数值

    ubuntu命令: dpkg -l 每条记录对应一个软件包,每条记录的第一,二,三个字符是软件包的状态标识,后边依此时软件包名称,版本号,和简述:   第一个字符为,期望值:包括如下状态: u 状态未 ...

  3. Fastadmin 后台表单,外键关键,步骤

    1.在 public\assets\js\backend 目录中找到对应的js,修改 2.对应控制器中加上index()方法:开启关联查询 重点: protected $relationSearch ...

  4. koa 路由、视图模块化(二)

    1.项目目录 2.路由 根目录/routes/index.js -- 首页 const router = require('koa-router')(); router.get('/', async ...

  5. Oracle 11g的日志路径

    Oracle数据库的最常用问题定位日志是alert日志,Oracle数据库的日志文件alert_$ORACLE_SID.log记录了重作日志的转换,数据库启动和关闭,数据库结构的改变,回退段的修改,死 ...

  6. 从GoogleClusterData统计每个用户的使用率、平均每次出价

    之前将google cluster data导入了Azure上的MySQL数据库,下一步就是对这些数据进行分析, 挖掘用户的使用规律了. 首先,为了加快执行速度,对user,time等加入索引. 然后 ...

  7. Mysql中用SQL增加、删除、修改(包括字段长度/注释/字段名)总结

    转: Mysql中用SQL增加.删除.修改(包括字段长度/注释/字段名)总结 2018年09月05日 10:14:37 桥Dopey 阅读数:1830   版权声明:本文为博主原创文章,未经博主允许不 ...

  8. 阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_3 配置解决中文乱码的过滤器

    输入中文 中文后台接收到 全部乱码 springMvc提供了过滤器 配置过滤器 characterEncodingFilter是首字母小写当做起的名称.当然这里也可以任意起名字.为了对应所以修改类名首 ...

  9. Implementing a Dynamic Vector (Array) in C(使用c实现动态数组Vector)

    An array (vector) is a common-place data type, used to hold and describe a collection of elements. T ...

  10. Autofac实现AOP拦截

    本文主要是详解一下在ASP.NET Core中,采用替换后的Autofac来实现AOP拦截. Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AO ...