94. Binary Tree Inorder Traversal(二叉树中序遍历)
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
递归:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
help(root);
return res;
}
private void help(TreeNode root){
if(root==null) return ;
if(root.left!=null)
help(root.left);
res.add(root.val);
if(root.right!=null)
help(root.right);
}
}
非递归:
终极版:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
while(root!=null||!s.isEmpty()){
while(root!=null){
s.push(root);
root=root.left;
}
if(!s.isEmpty()){
root = s.pop();
res.add(root.val);
root = root.right;
}
}
return res;
}
}
利用辅助桟
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while(cur!=null ||!stack.isEmpty()){
while(cur!=null){
stack.push(cur);
cur =cur.left;
}
cur = stack.pop();
res.add(cur.val);
cur =cur.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 | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- LeetCode OJ: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 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历
题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- Flask 安装 快速入门
$ pip install flask Flask自带的Server在端口5000上监听: ython app.py flask通过request.form['name']来获取表单的内容. 外部可见 ...
- XHTML XML
XHTML是可扩展的超文本标记语言(Extensible HyperText Markup Language). l XHTML是w3c组织在2000年的时候为了增强HTML推出的,本来是想替代HT ...
- Android无线测试之—UiAutomator UiObject API介绍七
判断对象是否存在 1.判断对象是否存在相关API 返回值 API 描述 boolean waitForExists(long timeout) 等待对象出现 boolean waitUntilGone ...
- Eclipse获取签名证书的SHA1
签名后的获取方式 :http://jingyan.baidu.com/article/3ea51489da3bc252e71bba59.html 未签名的获取方式:http://www.th7.cn/ ...
- 《从零开始学Swift》学习笔记(Day 61)——Core Foundation框架之内存管理
原创文章,欢迎转载.转载请注明:关东升的博客 在Swift原生数据类型.Foundation框架数据类型和Core Foundation框架数据类型之间转换过程中,虽然是大部分是可以零开销桥接,零开销 ...
- JS中:数组和二维数组、MAP、Set和枚举的使用
1.数组和二维数组: 方法一: var names = ['Michael', 'Bob', 'Tracy']; names[0];// 'Michael' 方法二: var mycars=new ...
- 转SpringMVC Controller 返回值的可选类型
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...
- svn实现共享文件夹/文件或svn文件夹/文件链接
我们在实际开发过程中,经常会遇到这种情况,多个项目组使用同一公共底层代码:公共底层代码是有专门负责人开发的,其它项目组只是使用即可,那么多个项目组就需要把公共底层代码放到各自的解决方案或目录文件下,解 ...
- 记录--jquery 获取父级、子级、兄弟元素 + 实例
需求如下: 三条数据,需点击其中一条数据在其下面展示与此数据关联的图片.主要功能可能是在点击的数据下增加显示行 思路: 把需要点击增加的数据先隐藏.点击后再将其显示出来. 知识点: jQuery.pa ...
- [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players
Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...