题目:

二叉树的后序遍历

给出一棵二叉树,返回其节点值的后序遍历。

样例

给出一棵二叉树 {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 二叉树的后序遍历的更多相关文章

  1. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  2. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  3. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  4. LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

  5. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  6. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  7. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  8. 145 Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

  9. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

随机推荐

  1. zip生成

    生成zip文件官方网站:http://www.phpconcept.net/pclzip/ 用法一: 1 <?php 2     include_once('pclzip.lib.php'); ...

  2. Discuz X3.2 SEO设置 title 不支持空格的解决方法

    很多使用 Discuz X3.2 的同学都发现这么一个问题:在后台SEO设置-title设定的时候,即使你在连字符两侧输入了空格,在前台也显示不出来,很多同学纠结这个问题,今天终于找到了解决方法,在此 ...

  3. Mac上安装brew

    用过ubuntu系统的都知道,上面有一个命令apt-get 很方便可以快速的安装很多软件 特别lamp环境 都是一键安装. 在mac上也有类似的命令 brew brew用法可以访问官网地址  http ...

  4. 关于CSS中的PX值(像素)

    场景: 人物:前端实习生「阿树」与 切图工程师「玉凤」事件:设计师出设计稿,前端实现页面 玉凤:树,设计稿发给你啦,差那么点像素,就叼死你┏(  ̄へ ̄)=☞阿树:~(>_<)~毛问题噶啦~ ...

  5. Messages.pas里的消息

    一.Windows 消息大全 这张表拷贝自万一兄的帖子:http://www.cnblogs.com/del/archive/2008/02/25/1079970.html 但是我希望自己能把这些消息 ...

  6. Lucene 3.0

    http://www.cnblogs.com/forfuture1978/archive/2010/02/22/1671487.html http://www.cnblogs.com/jiekzou/ ...

  7. makefile教程网址

    http://www.cnblogs.com/wang_yb/p/3990952.html

  8. 分布式文件系统 - FastDFS

    分布式文件系统 - FastDFS 别问我在哪里 也许我早已不是我自己,别问我在哪里,我一直在这里. 突然不知道说些什么了... 初识 FastDFS 记得那是我刚毕业后进入的第一家公司,一个技术小白 ...

  9. 利用QObject反射实现jsonrpc

    1.jsonrpc请求中的params数组生成签名 static QString signatureFromJsonArray(const QJsonArray &array) { QStri ...

  10. JDBC 学习笔记(三)—— 数据源(数据库连接池):DBCP数据源、C3P0 数据源以及自定义数据源技术

    本文目录:        1.应用程序直接获取连接的缺点(图解)        2.使用数据库连接池优化程序性能(图解)        3.可扩展增强某个类方法的功能的三种方式        4.自定 ...