Leetcode题目94.二叉树的中序遍历(中等)
题目描述:
给定一个二叉树,返回它的中序遍历。
示例: 输入: [1,null,2,3]
1
\
2
/
3 输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
思路解析:
1)递归:没啥说的,左子树->根->右子树顺序去遍历
2)迭代计算:用栈,再用一个指针模拟访问过程
代码实现:
1)递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
//返回该二叉树的中序遍历
public static List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>();
dfsTraversal(res, root);
return res;
} private static void dfsTraversal(List<Integer> res, TreeNode root) {
if (root == null) {
return;
}
dfsTraversal(res, root.left);
res.add(root.val);
dfsTraversal(res, root.right);
}
}
2)迭代:
public static List<Integer> inorderTraversal(TreeNode root) {
//借助一个占来实现
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) {
return res;
}
TreeNode pNode = root;
while (!stack.isEmpty() || pNode != null) {
while (pNode != null) {
stack.push(pNode);
pNode = pNode.left;
}
//出栈,弹出元素加入结果集
TreeNode node = stack.pop();
res.add(node.val);
pNode = node.right;
}
return res;
}
时间复杂度:O(n)。递归函数 T(n)=2⋅T(n/2)+1。
空间复杂度:最坏情况下需要空间O(n)(每个节点只有一颗子树),平均情况为O(logn)(满二叉树结构排列)。
Leetcode题目94.二叉树的中序遍历(中等)的更多相关文章
- 【LeetCode】94. 二叉树的中序遍历
94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- 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二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
- 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 中去 将当前节 ...
随机推荐
- 使用cnpm淘宝镜像
选装cnpm 1.说明:因为npm安装插件是从国外服务器下载,受网络影响大,可能出现异常,如果npm的服务器在中国就好了,所以我们乐于分享的淘宝团队干了这事. 2.官方网址:http://npm.ta ...
- 解决No module named 'sklearn.cross_validation'
sklearn中已经废弃cross_validation,将其中的内容整合到model_selection中 将sklearn.cross_validation 替换为 sklearn.model_s ...
- Centos7虚拟机根分区扩展
线上的kvm虚拟机,原来只规划了8G,后来发现硬盘动不动就被日志塞满了,需要进行扩容. 扩容步骤如下: 1.先把kvm虚拟机关机 2.在宿主机上进行kvm虚拟机的磁盘扩容 qemu-img resiz ...
- 桌面Ubuntu卡死解决方案
通常情况下,我们用桌面Ubuntu会遇到卡住的的情况,我们一般会进行强制关机处理,但其实还有另一种操作,不用强制关机. 切换到tty模式,执行命令pkill X;start X;就能重新进入桌面,不用 ...
- 移动端meta常用的设置
1.qq强制横屏或者竖屏显示 : <meta name="x5-orientation" content="portrait ||andscape&quo ...
- Girls Like You--Maroon 5
Girls Like You Spent 24 hours, I need more hours with you (24小时过去 还想和你 相处更久) You spent the weekend g ...
- es的相关知识二(检索文档)
一.es的使用 1.检索文档: 想要从Elasticsearch中获取文档,我们使用同样的 _index . _type . _id ,但是HTTP方法改为 GET : GET /{index ...
- HAL UART DMA 数据收发
UART使用DMA进行数据收发,实现功能,串口2发送指令到上位机,上位机返回数据给串口2,串口2收到数据后由串口1进行转发,该功能为实验功能 1.UART与DMA通道进行绑定 void HAL_UAR ...
- ftp上传文件和下载文件
public class FtpService { #region Fields and attributes private readonly int BufLen = 2048; /// < ...
- Hbuilder + MUI 的简单案例
话不多说 直接上代码 项目结构: index.html 的代码 <!DOCTYPE html><html> <head> <meta ch ...