lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目:
二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。
给出一棵二叉树 {1,#,2,3},
1
\
2
/
3
返回 [3,2,1]
你能使用非递归实现么?
解题:
递归程序好简单
Java程序:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
res = postorder(res,root);
return res;
}
public ArrayList<Integer> postorder(ArrayList<Integer> res,TreeNode root){
if(root==null)
return res;
if(root.left!=null)
res = postorder(res,root.left);
if(root.right!=null)
res = postorder(res,root.right);
res.add(root.val);
return res;
}
}
总耗时: 1210 ms
Python程序:
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
""" class Solution:
"""
@param root: The root of binary tree.
@return: Postorder in ArrayList which contains node values.
"""
def postorderTraversal(self, root):
# write your code here
res = []
res = self.postorder(res,root)
return res def postorder(self,res,root):
if root==None:
return res
if root.left!=None:
res = self.postorder(res,root.left)
if root.right!=None:
res = self.postorder(res,root.right)
res.append(root.val)
return res
总耗时: 380 ms
非递归程序,直接来源
Java程序:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
// write your code here
int a = 1;
ArrayList<TreeNode> s = new ArrayList<TreeNode>();
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) return res;
while(a == 1){
while(root.left != null || root.right != null){
if (root.left != null){
s.add(root);
root = root.left;
}
else{
s.add(root);
root = root.right;
}
}
TreeNode y = s.get(s.size()-1);
while (root == y.right || y.right == null){
res.add(root.val);
s.remove(s.size()-1);
if (s.size() == 0){
a = 0;
res.add(y.val);
break;
}
root = y;
y = s.get(s.size()-1);
}
if (root == y.left && y.right != null){
res.add(root.val);
root = y.right;
}
}
return res;
}
}
总耗时: 1388 ms
Python程序:
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
""" class Solution:
"""
@param root: The root of binary tree.
@return: Postorder in ArrayList which contains node values.
"""
def postorderTraversal(self, root):
# write your code here
a = 1
s = [root]
res = []
if root is None:
return res[1:1]
while a == 1:
while root.left is not None or root.right is not None:
if root.left is not None:
s.append(root)
root = root.left
else:
s.append(root)
root = root.right
y = s[len(s)-1]
while root == y.right or y.right is None:
res.append(root.val)
del s[len(s)-1]
if len(s) == 1:
a = 0
res.append(y.val)
break
root = y
y = s[len(s)-1]
if root == y.left and y.right is not None:
res.append(root.val)
root = y.right
return res
总耗时: 360 ms
lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历的更多相关文章
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- 145 Binary Tree Postorder Traversal 二叉树的后序遍历
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
随机推荐
- AngularJS(16)-路由
AngularJS 路由 本章节我们将为大家介绍 AngularJS 路由. AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web ...
- 通过百度地图API实现搜索地址--第三方开源--百度地图(三)
搜索地址功能是建立在能够通过百度地图API获取位置的基础上 通过百度地图定位获取位置详情:http://www.cnblogs.com/zzw1994/p/5008134.html package c ...
- easyui获取一行数据和修改data-options的值
<table id="tab" class="easyui-datagrid" style="width: 100%; height: 500p ...
- python学习第二天第二部分
一.变量:用来记录状态 变量值得变化即状态的变化,程序运行的本质就是来处理一系列状态的变化 python中所有数据都是对象 对象的三个特性: 身份(内存地址):用id()获取 类型:决定了该对象可以保 ...
- postgreSQL数据库(索引、视图)
索引的含义与特点 索引是一个单独的.存储在磁盘上的数据库结构,它们包含对数据所有记录的引用指针,postgresql列类型都可以被索引,对相关列索引是提高查询操作效率的最佳途径.例如,查询select ...
- Android开发——AsyncTask详解
android提供AsynvTask,目的是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的. Android为了降低开发难度,提供了AsyncTask.Asyn ...
- [css]兼容性
div +input 输入框 , 在微信中 有问题 块级元素 行内元素
- php正则表达式判断是否为ip格式
<?php $a = '127.0.0.111'; $b = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/",$a); ...
- npm,grunt,less,sass,typescript
typescript http://www.typescriptlang.org sass http://sass-lang.com/ less http://lesscss.org/ bow ...
- android 中使用回调方法(适用于自定义view传值到activity、adapter传值到activity)
如图所示: 每当listview中有选中的操作时都需要发消息给activity,用来实时改变真实需要支付的剩余金额. 代码暂不公开啦!公司项目!