leecode刷题(29)-- 二叉树的中序遍历
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)-- 二叉树的中序遍历的更多相关文章
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
- lintcode:二叉树的中序遍历
题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...
- LeetCode(94):二叉树的中序遍历
Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- LintCode-67.二叉树的中序遍历
二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
随机推荐
- 12.数值的整数次方 Java
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 这道题看似简单,其实BUG重重.要注意的问题: 1 关于次幂的问题特殊的情况, ...
- C++入门经典-例8.10-实现抽象类中的成员函数
1:抽象类通常作为其他类的父类,如果从抽象类派生的子类是抽象类,则子类必须实现父类中的所有纯虚函数.代码如下: // 8.10.cpp : 定义控制台应用程序的入口点. // #include &qu ...
- 解决分页插件ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.p ...
- tomcat manager 配置
使用网页部署新 Web 应用程序或取消现有 Web 应用程序部署,且无需重启容器. 一.开启管理 编辑 conf/tomcat-users.xml 添加如下内容,这里用户名和密码都为 tomcat & ...
- Java-AESUtil
在线版 http://tool.chacuo.net/cryptaes 要使用 AES/CBC/PKCS7Padding 模式需要添加依赖 <!--AES/CBC/PKCS7Padding--& ...
- koa 项目实战(一)创建项目
1.安装模块 npm install koa koa-router --save npm install -g nodemon 2.入口文件 app.js const Koa = require('k ...
- leetcode-hard-array-239. Sliding Window Maximum
mycode 89.27% class Solution(object): def maxSlidingWindow(self, nums, k): """ :type ...
- LC 652. Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 安德鲁1.2Ku全下125C波(CCTV4K除外)
闲来无事.没事挑战下难度. 前面高楼挡住了,一直没有试过125, 没想到1.2的锅能全下 机器:恐龙机dinobot4K+ 天线:安德鲁1.2ku 接送结果:除4K外所有高清 图片如下
- Numpy 库
可以直接通过pip安装. pip install numpy 1 NumPy的数值类型 每一种数据类型都有相应的转换函数.使用dtype属性可以查看数组的数据类型.如下. 2 数组操作 使用arang ...