LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
中文:二叉树的兴许遍历(左-右-根)。能用非递归吗?
递归:
public class BinaryTreePostorderTraversal {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return list;
list.addAll(postorderTraversal(root.left));
list.addAll(postorderTraversal(root.right));
list.add(root.val);
return list;
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}
非递归:
public List<Integer> postorderTraversal(TreeNode root){
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);//最后訪问
while(!stack.isEmpty()){
TreeNode current = stack.peek();
//根节点无子节点
if(current.left == null && current.right == null){
list.add(current.val);
stack.pop();
}
if(current.left != null){
stack.push(current.left);
current.left = null;
continue;
}
if(current.right != null){
stack.push(current.right);
current.right = null;
continue;
}
}
return list;
}
LeetCode——Binary Tree Postorder Traversal的更多相关文章
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- LeetCode Binary Tree Postorder Traversal(数据结构)
题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
随机推荐
- asp.net站点从2003服务器迁移到2008服务器出现定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节的问题解决
解决方法: 1.从4.0降到2.0. 2.直接删除整个节点,如下:
- 巧用chrome开发者工具
说明:截图中的Chrome版本为52,不同版本可能略有区别. 常用设置 开发时消除静态资源缓存不能立刻更新的困扰,勾选Disable cache即可 切换颜色显示格式 修改默认颜色显示格式,在Sett ...
- Linux基础学习1
安装问题 随意下载的:CentOS-5.5-i386-LiveCD-Release2.iso Live CD 是可以直接运行在内存当中的,而不是安装镜像. 如之前玩过的BT5一样,把BT5-LiveC ...
- dedecms 留言板中引用模板文件方法
最近在做一个用dedecms搭建的网站,客户提出要有留言板,dedecms带了一个留言板的模块,安装倒是十分简便,但装完后发现界面十分粗糙.装修比较简单,但是发现遇到一个问题:网站通用的导航栏无法显示 ...
- FTP经典常用命令
FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用FTP,都会遇到大量的FTP内部命令. 熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍 ...
- SqlServer 书目
1. http://www.cnblogs.com/CareySon/archive/2013/05/04/PlanCacheInSQLServerPart2.html(运行计划缓存) 2. http ...
- Python 可视化Twitter中指定话题中Tweet的词汇频率
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-8 @author: guaguastd @name: pl ...
- Reverse Linked List II -- 翻转部分链表
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...
- Java 使用StringBuffer注意
Stringbuffer使用注意 问题背景: 模拟客户端使用Socket请求服务器核心系统,核心系统正常响应,内容较大,近2715KB,大于2.6M多. 使用指定编码GBK来接收响应内容到过程中没 ...
- python判断字符串类型
s为字符串 s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False.(重点,这是字母数字一起判断的!!) s.isalpha() 所有字符都是字母,为真返回 Tur ...