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] ...
随机推荐
- CodeForces 468A Program F
Description Little X used to play a card game called "24 Game", but recently he has found ...
- iOS 获取当前月份的天数(转)
在这里我很鄙视百度,尼玛 竟然每一个我想要的结果...最后还是用google弄到的.日前又需要自己以后慢慢研究 1. 获取当前月份有多少天 NSCalendar *calendar = [NSCale ...
- CSS网页布局:网站整体居中 转
body{ margin:0 auto; padding:0 auto; text-align:center;} 只设置body属性对ie浏览器有效果,但是对火狐等不起作用 body{ ...
- 2013年8月份第4周51Aspx源码发布详情
迷你桌面闹钟源码 2013-8-27 [VS2010]功能介绍:实现了定时闹钟的功能,可以设置闹钟最前端显示.感兴趣的可以下载学习. BR个人博客系统(课程设计)源码 2013-8-27 [VS2 ...
- cf--1C
//Accepted 0 KB 60 ms //给出正多变形上的三个点,求正多形的最小面积 //记三个点之间的距离a,b,c; //由余弦定理得cosA //从而可求出sinA,和正多边形所在外接圆的 ...
- Canopy测试IPython控制台输出
Canopy测试IPython控制台输出
- 收藏的博客--PHP
32位Win7下安装与配置PHP环境(一至三) http://blog.csdn.net/yousuosi/article/details/9448903
- 深入理解SELinux
目录(?)[+] 1. 简介 SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC机制. Security-Enhanced Linux (SELinux)由以下两部分组 ...
- oracle的存储结构
表空间 当一个用户被创建以后,随之就要为用户分配数据存储的空间,这在oracle中成为“表空间”(Tablespace). 在数据库中创建用户时,基于应用性能和管理的考虑,最好为不同的用户创建独立的表 ...
- .NET项目框架(转)
摘要:本文描述了在用VS.NET进行B/S开发时采用的框架结构,一般建立类库项目和Web项目,在Web基本aspx页面类中调用类库中方法,同时在aspx页面类中不需要写任何对数据库操作的SQL代码,便 ...