leetcode 94 Binary Tree Inorder Traversal ----- java
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].
Note: Recursive solution is trivial, could you do it iteratively?
求二叉树的中序遍历,要求不是用递归。
先用递归做一下,很简单。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List result = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if( root == null)
return result;
getResult(root);
return result;
} public void getResult(TreeNode root){
if( root.left != null)
getResult(root.left);
result.add(root.val);
if( root.right != null)
getResult(root.right);
} }
不用递归,用栈实现也是很简单的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { List result = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if( root == null)
return result;
Stack stack = new Stack<TreeNode>();
TreeNode node = root;
while( true ){
if( node.left == null){
result.add(node.val);
if( node.right == null ){
if( stack.isEmpty() )
break;
else
node = (TreeNode) stack.pop();
}else{
node = node.right;
} }else{
TreeNode flag = node;
node = node.left;
flag.left = null;
stack.push(flag); }
} return result;
} }
leetcode 94 Binary Tree Inorder Traversal ----- java的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
- Leetcode 94. 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 (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 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二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 94. Binary Tree Inorder Traversal (Java)
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
随机推荐
- nginx初识
- 升级或安装 GNOME Shell
1.安装经典Gnome桌面系统 install gnome-session-fallbackinstall gnome-appletsinstall indicator-applet indicato ...
- Android程序之全国天气预报查询接口演示
一.项目演示效果如下: 二.使用 聚合数据SDK 注册账号-创建一个新应用(在个人中心页面-数据中心-申请数据)–填入自己的应用–找到分类–天气预报-全国天气预报 下载sdk (由于项目使用的是1点几 ...
- NSURLSession的使用
虽然在iOS7引入NSURLSession时,就知道NSURLConnection会最终被苹果放弃,但人总是喜欢做熟悉的事情,在NSURLConnection还可以使用时,就懒得学习这新玩意了,而且本 ...
- LIST 和 MAP
Collection和Map LIST 集合 arraylist arraylist源代码: 1.ArrayList 底层采用数组实现,当使用不带参数的构造方法生成 ArrayList 对象时,实际上 ...
- js面向对象(构造函数与继承)
深入解读JavaScript面向对象编程实践 Mar 9, 2016 面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术. 对JavaScript而言,其 ...
- 如何通过 GT 快速开始性能测试?
http://gt.tencent.com/docs/a/2.1/GTAndroidQuickStart.pdf Summary 安装 GT(GT.apk)后,不需要连接 PC 和在被测应用中插入代码 ...
- BZOJ 2331 地板
妈妈我会写插头dp了!!!!!!.... 感动啊... #include<iostream> #include<cstdio> #include<cstring> ...
- div+css文字垂直居中 解决左侧头像右侧姓名,姓名多换行后相对于头像仍居中显示
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- ModelFirst的CRUD
创建实体: