LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历
94. Binary Tree Inorder Traversal
题目描述
给定一个二叉树,返回它的 中序 遍历。
LeetCode94. Binary Tree Inorder Traversal
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
Java 实现
Iterative Solution
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
if (root == null) {
return result;
}
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();
result.add(cur.val);
cur = cur.right;
}
return result;
}
}
Recursive Solution
import java.util.LinkedList;
import java.util.List;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
List<Integer> result = new LinkedList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) {
return result;
}
inorderTraversal(root.left);
result.add(root.val);
inorderTraversal(root.right);
return result;
}
}
相似题目
- 98. 验证二叉搜索树
- 144. 二叉树的前序遍历
- 145. 二叉树的后序遍历
- 173. 二叉搜索树迭代器
- 230. 二叉搜索树中第K小的元素
- 272. 最接近的二叉搜索树值 II
- 285. 二叉搜索树中的顺序后继
- 426. 将二叉搜索树转化为排序的双向链表
- 783. 二叉搜索树结点最小距离
参考资料
- https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
- https://leetcode.com/problems/binary-tree-inorder-traversal/
LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)的更多相关文章
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- leetcode 94二叉树的中序遍历
递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【leetcode 94. 二叉树的中序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...
- LeetCode 94 ——二叉树的中序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...
随机推荐
- VMware workstation虚拟机与真机之间复制文件
首先选择导航栏的“虚拟机”选项,在下拉菜单中选择“安装VMware Tools” 弹出自动播放的选项,选择安装setup64.exe 开始安装VMware Tools,安装过程都保持默认即可,一直点 ...
- git 的使用方法以及要注意的地方~
1.假如你在一个分支,非master分支,例如avatar,在你修改之前一定要 get merge master,git pull,再开始写代码.如果改好了,也要先git merge master,g ...
- 小程序 跳转web-view 点击左上角返回需要点击2次才能返回
小程序 跳转web-view 点击左上角返回需要点击2次才能返回 再html页面引入js即可解决 <script type="text/javascript" src=& ...
- SWLU:主核性能采样、调试工具包
http://bbs.nsccwx.cn/topic/262/swlu-主核性能采样-调试工具包
- mediacoder固定质量CRF
视频编码:crf 与 bitrate 对照表 CRF(constant rate factor)就是x264/x265下压制视频的一种恒定量化值的编码方式,码率不恒定.其实就相当于vbr1pass.采 ...
- Vue实现图片预加载
<script>export default { data () { return { count: 0, } }, mounted: function() { this.preload( ...
- php redis set 单机锁(转)
如果一个请求更新缓存的时间比较长,甚至比锁的有效期还要长,导致在缓存更新过程中,锁就失效了,此时另一个请求会获取锁,但前一个请求在缓存更新完毕的时候,如果不加以判断直接删除锁,就会出现误删除其它请求创 ...
- Linux防火墙白名单设置
在linux系统中安装yum install iptables-services 然后 vi /etc/sysconfig/iptables # Generated by iptables-save ...
- Netty服务器连接池管理设计思路
应用场景: 在RPC框架中,使用Netty作为高性能的网络通信框架时,每一次服务调用,都需要与Netty服务端建立连接的话,很容易导致Netty服务器资源耗尽.所以,想到连接池技术,将与同一个Nett ...
- Python3基础 交换两个变量的值
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...