144. Binary Tree Preorder Traversal
来源:互联网 发布:关口知宏 对中国评价 编辑:程序博客网 时间:2024/06/06 10:49
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
題意:
對一個二叉樹做一個前序歷遍(請用迭代的方式)
題解(遞歸):
雖然題目要求使用迭代的方式,但我們還是先以遞歸的方式歷遍,以便於理解。遞歸的方式非常直觀,是以中(root)->左(root.left)->右(root.right)的方式來依序拜訪每個樹的節點,可以很容易地寫出下面的代碼:
package LeetCode.Medium;import java.util.ArrayList;import java.util.List;import LeetCode.Dependencies.TreeNode;/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } * 中左右 */public class BinaryTreePreorderTraversal_Recursive { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); if(root == null) return result; helper(root, result); return result; } void helper(TreeNode root, List<Integer> result) { //若此節點沒有值則返回 if(root == null) return; //將此節點放入結果當中 result.add(root.val); //進行左子樹的歷遍 if(root.left != null) helper(root.left, result); //進行右子樹的歷遍 if(root.right != null) helper(root.right, result); }}
但依據題目要求,我們仍需要使用迭代來解這一道題,故我們利用Stack(棧)的思想來解這道題,棧的思想是先進後出,我們可以寫出下面的迭代步驟:
1.將首節點加入stack中
=====(直到stack為空)========
2.將stack排出一個,並指定為root
3.將root的值加入結果
4.將root.right放入stack(有左比右先拜訪的效果)
5.將root.left放入stack
=====(stack為空後)==========
6.輸出結果
ex:
1 stack result
/ \ =========================
2 3 step0 1 null
/ \ step1 3,2 1
4 5 step2 3,5,4 1,2
step3 3,5 1,2,4
step4 3 1,2,4,5
step5 null 1,2,4,5,3
package LeetCode.Medium;import java.util.ArrayList;import java.util.List;import java.util.Stack;import LeetCode.Dependencies.TreeNode;/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } * 中左右(迭代版) */public class BinaryTreePreorderTraversal {/* * 1.將首節點加入stack中 * =====(直到stack為空)======== * 2.將stack排出一個,並指定為root * 3.將root的值加入結果 * 4.將root.right放入stack(有左比右先拜訪的效果) * 5.將root.left放入stack * =====(stack為空後)========== * 6.輸出結果ex: 1 stack result / \ ========================= 2 3 step0 1 null/ \ step1 3,2 1 4 5 step2 3,5,4 1,2 step3 3,5 1,2,4 step4 3 1,2,4,5 step5 null 1,2,4,5,3*/ public List<Integer> preorderTraversal(TreeNode root) { //用來存儲結果 List<Integer> result = new ArrayList<Integer>(); //對於root == null的情況 if(root == null) return result; //利用stack來進行樹的歷遍 Stack<TreeNode> stack = new Stack<TreeNode>(); //一定要將第一個節點放入stack中 stack.push(root); while(stack.empty() == false) { //先推出stack最後一個加入的TreeNode root = stack.pop(); //加入結果當中 result.add(root.val); //將右節點放入stack中(因為先進後出,故右邊會比左邊慢被排出來) if(root.right != null) { stack.push(root.right); } //將左節點放入stack中(因為先進後出,故左邊會比右邊快被排出來) if(root.left != null) { stack.push(root.left); } } //返回答案 return result; }}
- 144.Binary Tree Preorder Traversal
- 144.Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144.Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- git报错--RPC failed; curl 18 transfer closed with outstanding read data remaining
- js小数转换百分数
- 排序功能的实现
- 在QT对话模中用webengine 无法加载WEB页的问题
- 查看webjoin下的tomcat的版本
- 144. Binary Tree Preorder Traversal
- Android中用TextView显示大量文字的方法
- php视频上传
- maven学习笔记(五) maven的依赖
- poj 3070 Fibonacci,不用打表的快速斐波那契
- Python ML环境搭建与学习资料推荐
- python常用语法
- 修改z-stack LED引脚出现引脚不可控
- hdu6092 Rikka with Subset(母函数+思维)