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?
import java.util.*;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<TreeNode> stack2 = new Stack<TreeNode>();
stack.add(root);
while(!stack.isEmpty()){
TreeNode r = stack.pop();
if(r.left!=null)
stack.add(r.left);
if(r.right!=null)
stack.add(r.right);
stack2.add(r);
}
while(!stack2.isEmpty()){
list.add(stack2.pop().val);
}
//LRD(list,root);递归
return list;
}
public void LRD(ArrayList<Integer> list,TreeNode root){
if(root==null)
return;
LRD(list,root.left);
LRD(list,root.right);
list.add(root.val);
}
}
LeetCode 二叉树后序遍历(binary-tree-postorder-traversal)的更多相关文章
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)
590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- 【Leetcode】【hard】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- html 实现网址链接
<a href="http://acm.nyist.net/JudgeOnline/problemset.php">南工oj</a> HTML学习 < ...
- tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
需要在配置文件里加上下面就ok了 <property name=”minEvictableIdleTimeMillis” value=”1800000″ /> <property n ...
- 利用svg技术实现在线动画演示
搜索MDCC的论文,发现了这个站点,里面有演示动画,居然是通过svg来实现的. 分享给大家看看: 有空研究下,做一个类似的演示,展示一下OceanBase内部的常见操作. 展示一个svg做的游戏: h ...
- 源码安装rsyslog
<pre name="code" class="html">下载下列软件 json-c-0.12-20140410.tar.gz---------- ...
- axis1客户端调用webservice的通用代码
1.axis1 作为web service 客户端时,调用web service 服务端的通用代码 String url = "http://www.webxml.com.cn/webser ...
- iPhone 6 为何坚持1GB内存?
原文地址:http://digi.ifeng.com/expert/special/96/#6467378-qzone-1-9015-46cf52f061fd6e814686a918cedcb024 ...
- hdu-Common Subsequence
http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description A subsequence of a given sequence ...
- FPGA STA(静态时序分析)
1 FPGA设计过程中所遇到的路径有输入到触发器,触发器到触发器,触发器到输出,例如以下图所看到的: 这些路径与输入延时输出延时,建立和保持时序有关. 2. 应用背景 静态时序分析简称STA,它是一种 ...
- c++栈管理库TCMalloc、jeMalloc
示例:http://blog.csdn.net/chosen0ne/article/details/9338591
- css 3种清除浮动方法
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...