LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
SOL:
包括递归与非递归方法:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal1(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
rec(root, ret);
return ret;
} public void rec(TreeNode root, List<Integer> ret) {
if (root == null) {
return;
} rec(root.left, ret);
ret.add(root.val);
rec(root.right, ret);
} public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if (root == null) {
return ret;
} Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode cur = root; while (true) {
while (cur != null) {
s.push(cur);
cur = cur.left;
} if (s.isEmpty()) {
break;
} cur = s.pop();
ret.add(cur.val); cur = cur.right;
} return ret;
}
}
LeetCode: Binary Tree Inorder Traversal 解题报告的更多相关文章
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- Leetcode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode] Binary Tree Inorder Traversal 中序排序
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- spring hiberante 集成出现异常 java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
出现如题的异常是由于hibernate和spring集成时的的版本不一致所导致. 如下面,所示,如果你用的hibneate 4.0及以上版本,那么将会报错,因为这里用的事务管理是hibernate 3 ...
- 集成支付宝钱包支付 iOS SDK 的方法与经验
下载 首先,你要想找到这个SDK,都得费点功夫.现在的SDK改名叫移动支付集成开发包了,下载页面在 这里 (http://t.cn/8ksiklD)的 “请点此下载集成开发包(http://t.cn/ ...
- ubuntu 51单片机环境搭建方法
首先下载个sdcc 1: sudo apt-get install sdcc 2: sudo apt-get install libvte-dev3: 安装 gSTC-ISP 下载地址:http:// ...
- windbg cs
!cs 扩展显示一个或多个临界区(critical section)或者整个临界区树 !cs Address 指定要显示的临界区地址.如果省略该参数,调试器显示当前进程中所有临界区. :> ...
- 为Magento1.5新增会员注册字段(转)
第一步.新建一个模块,在app/etc/modules/目录下新建文件Shuishui_Customer.xml <config> <modules> <Shuishui ...
- MVC摘记
2013年9月23日18:04:06 直接cshtml页面的js中使用 var customerId = @(ViewBag.Oppotunity as OppotunityDto==null ...
- ASP.NET项目在IIS上使用虚拟目录
在IIS中,应用程序与虚拟目录特别容易混淆,但两者又是完全不同的概念. 应用程序是一个逻辑边界,这个边界可以分隔网站及其组成部分.虚拟目录则是一个真实的指针,这个指针指向了一个本地或远程的物理路径.虚 ...
- 【转】java原理—反射机制
一.什么是反射:反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力.这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语言 ...
- Android开发:setAlpha()方法和常用RGB颜色表----颜色, r g b分量数值(int), 16进制表示 一一对应
杂家前文Android颜色对照表只有颜色和十六进制,有时候需要设置r g b分量的int值,如paint.setARGB(255, 127, 255, 212);就需要自己计算下分量的各个值.这里提供 ...
- excel中对数据进行分类求和
我们在用excel处理数据时,常常需要按不同的类别分别汇总数据.例如下图中需要求出每个业务员的总销售金额等. 通常情况下我们的数据量很大,而且需要较快的统计出来结果,所以我们要用一定的技巧才能计算出来 ...