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] ...
随机推荐
- [开发笔记]-控制Windows Service服务运行
用代码实现动态控制Service服务运行状态. 效果图: 代码: #region 启动服务 /// <summary> /// 启动服务 /// </summary> /// ...
- Android 获取网络状态
1.检测网络是否可用 public boolean isNetWorkConnected() { ConnectivityManager cm = (ConnectivityManager)getSy ...
- android baseApplication 基类
package com.free.csdn.base; import java.io.File;import java.util.ArrayList;import java.util.List; im ...
- java基础之 反射
首先,我们在开始前提出一个问题: 1.在运行时,对于一个java类,能否知道属性和方法:能否去调用它的任意方法? 答案是肯定的. 本节所有目录如下: 什么是JAVA的反射机制 JDK中提供的Refle ...
- JavaScript编程异步助手:Promise
异步模式在Web编程中变得越来越重要,对于Web主流语言JavaScript来说,这种模式实现起来不是很利索,为此,许多JavaScript库(比如 jQuery和Dojo.AngularJS)添加了 ...
- JAVA之关于This的用法
JAVA之关于This的用法 业精于勤,荒于嬉:行成于思,毁于随.——韩愈 用类名定义一个变量的时候,定义的应该只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法,那们类里面是够也应该 ...
- oracle触发器的小例子
实现功能: 插入数据前触发,检查与插入数据几个属性相同的在表中的列将状态改为false,再执行插入. 解决方案: CREATE OR REPLACE TRIGGER tri_insert BEFORE ...
- 有哪些 PHP 调试技巧?
我目前遇到的最让我称赞的debug方式是:xdebug的 xdebug_start_trace(); /* 业务代码 */ xdebug_stop_trace(); 他解决了我长久以来一个代码调试问题 ...
- swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象
欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...
- 深入C#数据类型小部分第二章
值类型和引用类型C#的值类型包括:结构体(数值类型,bool型,用户定义的结构体),枚举,可空类型. C#的引用类型包括:数组,用户定义的类.接口.委托,object,字符串. 数组的元素,不管是引用 ...