Binary Tree Postorder Traversal 二叉树后续遍历@LeetCode
来源:互联网 发布:got it 和get it 编辑:程序博客网 时间:2024/05/17 09:38
迭代解法在那篇面试总结二中写了
package Level3;import java.util.ArrayList;import Utility.TreeNode;/** * 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 / 3return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively? * */public class S134 {public static void main(String[] args) {}public ArrayList<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> ret = new ArrayList<Integer>();if(root == null){return ret;}rec(root, ret);return ret; }public void rec(TreeNode root, ArrayList<Integer> ret){if(root == null){return;}rec(root.left, ret);rec(root.right, ret);ret.add(root.val);}}
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public ArrayList<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> ret = new ArrayList<Integer>(); if(root == null){ return ret; } Stack<TreeNode> s1 = new Stack<TreeNode>(); Stack<TreeNode> s2 = new Stack<TreeNode>(); s1.push(root); while(!s1.isEmpty()){ TreeNode cur = s1.pop(); s2.push(cur); if(cur.left != null){ s1.push(cur.left); } if(cur.right != null){ s1.push(cur.right); } } while(!s2.isEmpty()){ ret.add(s2.pop().val); } return ret; }}
- Binary Tree Postorder Traversal 二叉树后续遍历@LeetCode
- leetcode 145. Binary Tree Postorder Traversal 二叉树后续遍历
- 【二叉树的后续遍历】Binary Tree Postorder Traversal
- leetcode---binary-tree-postorder-traversal---树后续遍历
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)
- [leetcode]Binary Tree Postorder Traversal 二叉树后序非递归遍历
- Leetcode: Binary Tree Postorder Traversal(二叉树后序遍历)
- Leetcode Binary Tree Postorder Traversal 二叉树后序遍历
- LeetCode 145. Binary Tree Postorder Traversal(二叉树后序遍历)
- 145.leetcode Binary Tree Postorder Traversal(easy)[二叉树后序遍历]
- [leetcode-二叉树后序遍历]--145. Binary Tree Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal(二叉树后序遍历)
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal (利用中序遍历和后续遍历确定一颗二叉树)
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- Binary Tree Postorder Traversal-二叉树的后序遍历
- Binary Tree Postorder Traversal 二叉树的后序遍历
- 二叉树的后序遍历 Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal 二叉树的后序遍历
- SAP生产订单预留相关的备忘录
- WinSock 异步I/O模型[1]---选择模型 - select
- 贪心算法_哈夫曼编码问题(Huffman Coding)
- XP无法远程桌面连接解决方案
- 【玩转cocos2d-x之一】V2.2.0版本开发环境配置
- Binary Tree Postorder Traversal 二叉树后续遍历@LeetCode
- OpenStack_Havana_Install_Guide 多节点安装总结2
- 我的Python3学习笔记(1):python基础
- 一个牛人给java初学者的建议(很有意义,转载的)
- Sql结果导出为excel文件
- ios 入门开发三
- Android Sdk更新速度慢解决(转载)
- 一个MapReuce作业的从开始到结束--第2章 启动Hadoop
- 第十三周上机实践项目——fibnacci序列(递归法)