94. Binary Tree Inorder Traversal
来源:互联网 发布:贵州毕节网络诈骗犯 编辑:程序博客网 时间:2024/06/08 14:08
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
題意:
對一個二叉樹做一個中序歷遍(請用迭代的方式)
題解(遞歸):
雖然題目要求使用迭代的方式,但我們還是先以遞歸的方式歷遍,以便於理解。遞歸的方式非常直觀,是以左(root.left)->中(root)->右(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 BinaryTreeInorderTraversal_Recursive { public List<Integer> inorderTraversal(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; //進行左子樹的歷遍 helper(root.left, result); //將此節點放入結果當中 result.add(root.val); //進行右子樹的歷遍 helper(root.right, result); }}
題解(迭代):
但依據題目要求,我們仍需要使用迭代來解這一道題,故我們利用Stack(棧)的思想來解這道題,棧的思想是先進後出,我們可以寫出下面的迭代步驟:
1.將首節點加入stack中
=====(直到stack為空)========
2.if 當前節點不為空(還沒拜訪到當前子樹的最左節點)
2.1將root.left放入stack
2.2將root指定為root.left
else(繼續拜訪右子樹)
2.1將root指定為stack排出的節點
2.2將root的值加入結果
2.3將root的值指定為root.right
=====(stack為空後)==========
3.將結果的最後一個值(多出的)刪除
4.輸出結果
ex:
4 stack result
/ \ =============================
2 5 step0 4 null
/ \ step1 4,4 null
1 3 step2 4,4,2 null
step3 4,4,2,1 null
step4 4,4,2 1
step5 4,4,3 1,2
step6 4,5 1,2,3
step7 4 1,2,3,4
step8 null 1,2,3,4,5,4
step9 null 1,2,3,4,5
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 BinaryTreeInorderTraversal {/* * 1.將首節點加入stack中 * =====(直到stack為空)======== * 2.if 當前節點不為空(還沒拜訪到當前子樹的最左節點) * 2.1將root.left放入stack * 2.2將root指定為root.left * else(繼續拜訪右子樹) * 2.1將root指定為stack排出的節點 * 2.2將root的值加入結果 * 2.3將root的值指定為root.right * =====(stack為空後)========== * 3.將結果的最後一個值(多出的)刪除 * 4.輸出結果ex: 4 stack result / \ ============================= 2 5 step0 4 null/ \ step1 4,4 null 1 3 step2 4,4,2 null step3 4,4,2,1 null step4 4,4,2 1 step5 4,4,3 1,2 step6 4,5 1,2,3 step7 4 1,2,3,4 step8 null 1,2,3,4,5,4 step9 null 1,2,3,4,5 */ public List<Integer> inorderTraversal(TreeNode root) { //用來存儲結果 List<Integer> result = new ArrayList<>(); //對於root == null的情況 if(root == null) return result; //利用stack來進行樹的歷遍 Stack<TreeNode> stack = new Stack<>(); //一定要將第一個節點放入stack中 stack.push(root); while(stack.isEmpty() == false) { //若root不為空的情況(還沒拜訪到當前子樹的最左節點) if(root != null) { stack.push(root); root = root.left; //若root為空的情況(繼續拜訪右子樹) } else if(root == null){ root = stack.pop(); result.add(root.val); root = root.right; } } //將結果的最後一個值(多出的)刪除 result.remove(result.size() - 1); return result; }}
- 94. Binary Tree Inorder Traversal
- 94.Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94.Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- Android系统进程间通讯Binder机制
- 仿switch风格滑动按钮
- ubuntu常用软件的相关配置
- MySQL及Oracle中将varchar类型转换成int类型
- Code_vs_1959_拔河比赛
- 94. Binary Tree Inorder Traversal
- SSMybatis整合 -- 加入SpringMVC进行多表级联操作
- Webstorm常用快捷键储备
- 配置yum源为光盘
- 360加固助手实现多渠道打包终极讲解
- 之前的mysql与5.7.19的区别
- PopUpWindow使用详解(一)——基本使用
- html5前端实现文件分割上传
- Java中Arraylist为什么是查找快,增删慢?