94.Binary Tree Inorder Traversal---二叉树中序非递归遍历
题目大意:中序遍历二叉树。先序见144,后序见145。
法一:DFS,没啥说的,就是模板DFS。代码如下(耗时1ms):
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
dfs(res, root);
return res;
}
private void dfs(List<Integer> res, TreeNode root) {
if(root != null) {
dfs(res, root.left);
res.add(root.val);
dfs(res, root.right);
}
}
法二:非递归。与先序类似。代码如下(耗时2ms):
public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
TreeNode tmp = root;
while(!s.isEmpty() || tmp != null) {
//压入左孩子结点
while(tmp != null) {
s.push(tmp);
tmp = tmp.left;
}
//如果栈非空,弹出顶结点,遍历右子树
if(!s.isEmpty()) {
tmp = s.pop();
res.add(tmp.val);
tmp = tmp.right;
}
}
return res;
}
94.Binary Tree Inorder Traversal---二叉树中序非递归遍历的更多相关文章
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历
题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- BZOJ 2039 人员雇佣(最小割)
最小割的建图模式一般是,先算出总收益,然后再通过网络模型进行割边减去部分权值. 然后我们需要思考什么才能带来收益,什么才能有权值冲突. s连向选的点,t连向不选的点,那么收益的减少量应该就是将s集和t ...
- C++解析(8):C++中的新成员
0.目录 1.动态内存分配 1.1 C++中的动态内存分配 1.2 new关键字与malloc函数的区别 1.3 new关键字的初始化 2.命名空间 2.1 作用域与命名空间 2.2 命名空间的定义和 ...
- CentOS 7下安装pptp服务端手记 ok
主要配置步骤 1. 安装前检查系统支持 2. 安装必要包 3. 修改相关配置文件 4. 设置开机自动启动 pptpd, iptables 5. iptables配置网络 6. 阿里云ECS可能还需要几 ...
- Spring Cloud Greenwich 新特性和F升级分享
2019.01.23 期待已久的Spring Cloud Greenwich 发布了release版本,作为我们团队也第一时间把RC版本替换为release,以下为总结,希望对你使用Spring Cl ...
- Spring Boot系列教程一:Eclipse安装spring-tool-suite插件
一.前言 一直使用eclipse,个人习惯选用Eclipse+spring-tool-suite进行开发,特别注意Eclipse要选用对应的spring-tool-suite进行安装,这 ...
- 洛谷 P1023 税收与补贴问题 (2000NOIP提高组)
洛谷 P1023 税收与补贴问题 (2000NOIP提高组) 题意分析 一开始没理解题意.啰啰嗦嗦一大堆.看了别人的题解才明白啥意思. 对于样例来说,简而言之: 首先可以根据题目推算出来 28 130 ...
- bzoj3748 Kwadraty
Claris 当然是要用来%的 但是,,其他dalao,,比如JL的红太阳commonc.题解能不能稍微加几句话,蒟蒻看不懂啊. 在这里解释一下,Claris的题解.(因为我弱,想了半天才明白,所以觉 ...
- 【2016北京集训】Mushroom
Portal --> broken qwq Description 一开始有个蘑菇,蘑菇里面有\(n\)个房间,是一棵有根树,\(1\)号是根,每个房间里面都有杂草,现在要支持以下操作:将某个指 ...
- 公告:开通csdn博客,敬请关注!
公告:开通csdn博客,敬请关注!地址:https://blog.csdn.net/cyjch
- Codeforces243A The Brand New Function
A. The Brand New Function time limit per test 2 seconds memory limit per test 256 megabytes input st ...