144. Binary Tree Preorder Traversal
2017-08-10 10:50
190 查看
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
題意:
對一個二叉樹做一個前序歷遍(請用迭代的方式)
題解(遞歸):
雖然題目要求使用迭代的方式,但我們還是先以遞歸的方式歷遍,以便於理解。遞歸的方式非常直觀,是以中(root)->左(root.left)->右(root.right)的方式來依序拜訪每個樹的節點,可以很容易地寫出下面的代碼:
題解(迭代):
但依據題目要求,我們仍需要使用迭代來解這一道題,故我們利用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
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 (Tree, Stack)
- 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