lintcode,二叉树的后序遍历
来源:互联网 发布:linux目录结构详细介绍 编辑:程序博客网 时间:2024/05/06 11:04
给出一棵二叉树,返回其节点值的后序遍历。
样例
给出一棵二叉树 {1,#,2,3},
1
\
2
/
3
返回 [3,2,1]
挑战
你能使用非递归实现么?
解题思路:递归方法容易实现。非递归考虑用两个栈,类似栈实现队列的思路,将二叉树后序入栈。
一刷ac
/** * 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) { ArrayList<Integer> rst = new ArrayList<Integer>(); if(root == null) return rst; ArrayList<Integer> left = postorderTraversal(root.left); ArrayList<Integer> right = postorderTraversal(root.right); rst.addAll(left); rst.addAll(right); rst.add(root.val); return rst; }}
非递归两个栈。
/** * 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) { ArrayList<Integer> res = new ArrayList<Integer>(); if(root == null) return res; Stack<TreeNode> stack1 = new Stack<TreeNode>(); Stack<TreeNode> stack2 = new Stack<TreeNode>(); stack1.push(root); while(!stack1.isEmpty()){ TreeNode node = stack1.pop(); stack2.push(node); if(node.left != null)stack1.push(node.left); if(node.right != null)stack1.push(node.right); } while(!stack2.isEmpty()){ res.add(stack2.pop().val); } return res; }}
非递归一个栈
/** * 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) { ArrayList<Integer> res = new ArrayList<Integer>(); if(root == null) return res; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.empty()){ TreeNode node = stack.pop(); res.add(0,node.val); if(node.left != null) stack.push(node.left); if(node.right != null) stack.push(node.right); } return res; }}
非递归一个栈
/** * 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) { ArrayList<Integer> res = new ArrayList<Integer>(); if(root == null) return res; Stack<TreeNode> stack = new Stack<TreeNode>(); while(root != null){ res.add(0,root.val); if(root.left != null) stack.push(root.left); root = root.right; if(root == null && !stack.empty()){ root = stack.pop(); } } return res; }}
0 0
- LintCode -- 二叉树的后序遍历
- Lintcode 二叉树的后序遍历
- LintCode:二叉树的后序遍历
- lintcode,二叉树的后序遍历
- LintCode-二叉树的后序遍历
- lintcode 二叉树的后序遍历
- LintCode 二叉树的后序遍历
- 二叉树的后序遍历-LintCode
- lintcode二叉树的后序遍历
- LintCode 二叉树的后序遍历
- lintcode--二叉树的后序遍历
- LintCode-剑指Offer-(68)二叉树的后序遍历
- Lintcode 68 二叉树的后序遍历
- LintCode | 68. 二叉树的后序遍历
- lintcode——二叉树的后序遍历
- lintcode 68 二叉树的后序遍历
- LintCode 67 二叉树的后序遍历
- [LintCode]68.二叉树的后序遍历
- 禁止ScrollView自动滚动
- 实习每日总结_20161215
- OpenWRT(二)配置WAN口和LAN口
- 浅谈算法-简单倍增及st表的应用
- 考试周终于结束了
- lintcode,二叉树的后序遍历
- Android Studio 使用小技巧和快捷键
- ViewPager+GridView
- Primer Plus第一章总结
- Java.lang软件包中的类
- 关于opentack的(3)--nova api简析
- 最大k乘积问题
- iOS 面试题(6.):自己写的 view 成员,应该用 weak 还是 strong?
- 据说年薪30万的Android程序员必须知道的帖子