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 ...
随机推荐
- Autolayout 01
Auto Layout Concepts auto layout的基本概念是constraint(约束).表示了你interface中的layout元素.例如,你可以创建一个约束来指定元素的宽度或者距 ...
- django网站搭建常用的一些代码
from functools import wrapsdef check_user_login(func): @wraps(func) def return_wrapper(request, *arg ...
- python遍历当前目录并删除某文件
#coding: utf-8 """ this programe is to clear driverlog below this dir __author__:the_ ...
- Injection of resource dependencies failed解决办法总结
今天调试项目代码,出现的引resource的报错,查原因查了好长时间才找到,现在这里总结一下,以免以后忘掉以及给大家参考. 报错大致内容入下: org.springframework.beans.fa ...
- SparkStreaming和Drools结合的HelloWord版
关于sparkStreaming的测试Drools框架结合版 package com.dinpay.bdp.rcp.service; import java.math.BigDecimal; impo ...
- 【重点突破】—— Vue1.0到Vue2.0的变化
前言: 本文参考作者:_So_ 和 我是某慧 的博文,重点梳理Vue1.0升级到Vue2.0后在开发中要注意的不同,以做学习. 组件模板不再支持片段代码,必须有一个顶级元素包裹,例如: ...
- 2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源
学习资料:<Activiti实战> 第五章 用户与组及部署管理(二)部署流程资源 内容概览:讲解流程资源的读取与部署. 5.2 部署流程资源 5.2.1 流程资源 流程资源常用的有以下几种 ...
- JAVA Eclipse如何安装Swing
查看自己的Eclipse版本 打开WINDOWBUILDER的下载页面,找到自己对应版本的下载地址,注意只是一个下载地址,不是要下载东西 http://www.eclipse.org/window ...
- JAVA Eclipse开发Android程序如何自定义图标
直接用做好的png图片替换res的所有分辨率的lc_launcher.png图片 注意到不同文件夹有不同的分辨率,直接把png图片做成最大的替换掉即可,不管小的. drawable-xxhdpi ...
- JAVA_Could not find property [struts.actionMapping]怎么办
你的项目中不包含log4j.jar这个文件,包含进去即可