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 ...
随机推荐
- 删除select中所有option选项jquery代码
select中所有option选项如何删除,本文使用jquery简单实现下,有此需求的朋友可以参考下,希望对大家有所帮助. 这样写 复制代码代码如下: <select id="sear ...
- PHP获取和操作配置文件php.ini的几个函数
当无法修改php.ini配置文件怎么办,莫担心. php有一套设置和获取配置信息的函数. 1.ini_get()获取配置参数,ini_set()设置配置参数 <?php
- 用“逐步排除”的方法定位Java服务线上“系统性”故障(转)
一.摘要 由于硬件问题.系统资源紧缺或者程序本身的BUG,Java服务在线上不可避免地会出现一些“系统性”故障,比如:服务性能明显下降.部分(或所 有)接口超时或卡死等.其中部分故障隐藏颇深,对运维和 ...
- python杂记-5(装饰器)
1.被装饰的函数有参数(一个参数): def w1(func): def inner(arg): # 验证1 # 验证2 # 验证3 return func(arg) return inner @w1 ...
- 1017. Queueing at Bank (25)
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
- poll实现
struct pollfd { int fd; //当前描述符 short events; //进程关心的该描述符的事件 short revents; //返回 ...
- iOS7之定制View Controller切换效果
在iOS5和iOS6前,View Controller的切换主要有4种: 1. Push/Pop,NavigationViewController常干的事儿 2. Tab,TabViewControl ...
- [转]一个基于完成端口的TCP Server Framework,浅析IOCP
[转]一个基于完成端口的TCP Server Framework,浅析IOCP http://www.cppblog.com/adapterofcoms/archive/2010/06/26/1187 ...
- Laravel5 路由问题 /home页面无法访问
参考网址:http://stackoverflow.com/questions/11791375/laravel-routes-not-working Laravel5 路由问题 /home页面无法访 ...
- C与C++存储空间布局
每个程序一启动都有一个大小为4GB的内存,这个内存叫虚拟内存,是概念上的,真正能用到的,只是很小一部分,一般也就是在几百K到几百M.我们PC中内存,我们称之为物理内存,也就是256M,512M等,虚拟 ...