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

示例:

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

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

递归的思路很简单,不再累述,迭代的方法请参考百度。

对中序遍历的定义参考 https://baike.baidu.com/item/%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86/757281?fr=aladdin

代码如下:

 class Solution {
List<Integer> ans=new ArrayList<>(); public List<Integer> inorderTraversal(TreeNode root) { midfs(root);
return ans;
} private void midfs(TreeNode root) {
if(root==null)
return;
midfs(root.left);
ans.add(root.val);
midfs(root.right);
}
}

领扣(LeetCode)二叉树的中序遍历 个人题解的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. Leetcode题目94.二叉树的中序遍历(中等)

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

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

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

  8. 【LeetCode】94. 二叉树的中序遍历

    94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...

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

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

随机推荐

  1. Codeforces Round #426 The Meaningless Game

    题目网址:http://codeforces.com/contest/834/problem/C 题目: C. The Meaningless Game Slastyona and her loyal ...

  2. hadoop2.x的安装

    可以自己从官网编译打包也可以直接下载官网的.gz包.自己编译打包的过程如下: .查看是否安装cmake.svn.openssl.ncurses,没有的直接安装上 yum list|grep cmake ...

  3. spring boot 日志收集整理

    spring boot 日志 1. 选择那种日志框架 slf4j 是抽像的接口层.也是spring boot 默认采用的接口层. util.logging,log4j,logback,commons- ...

  4. Git 项目提交新仓库

    提示:进入项目文件操作 步骤: 1.git init   ----------初始化git仓库 2.git remote add origin 你的项目地址  ------------------如: ...

  5. Hadoop 在 windows 7 64位的配置(一)|非cygwin

    参照原文   http://blog.csdn.net/supperman_009/article/details/39991809 环境: Hadoop-2.4.1 Windows 7 64位 jd ...

  6. TCP UDP基本编程(一)

    tcp udp均可以用来网络通信,在使用之前建议先搜索一下相关网络连接的基本知识,可以更好的理解和使用,tcp建议看下如下文章:https://blog.csdn.net/chuangsun/arti ...

  7. fenby C语言 P9

    逻辑运算 真1 假0 &&与 真&&真为真 真&&假为假 假&&真为假 假&&假为假 ||或 真||真为真 真||假为真 ...

  8. IL2CPP深入详解-总览

    导语 该系列将会分为以下几个部分:1. 总览(本文)2. c++代码解析3. 调试c++代码4. 方法调用(一般方法,虚方法等)5. 泛型共享6. 类型与方法的 P/invoke 封装7. 垃圾回收8 ...

  9. 第三十七章 POSIX线程(一)

    POSIX线程库相关介绍   与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都有"pthread_"开头   要使用这些函数库,都需要加入头文件"<pth ...

  10. (大模拟紫题) Luogu P1953 易语言

    原题链接:P1953 易语言 (我最近怎么总在做大模拟大搜索题) 分别处理两种情况. 如果只有一个1或0 直接设一个cnt为这个值,每次输入一个新名字之后把数字替换成cnt,最后cnt++即可. 注意 ...