Leetcode111——二叉樹的最小深度
阿新 • • 發佈:2021-01-10
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/solution/er-cha-shu-de-zui-xiao-shen-du-by-leetcode-solutio/
DFS和BFS都屬於盲目搜尋。
DFS(Depth-First-Search):深度優先搜尋
DFS沿著樹的深度遍歷樹的節點,儘可能深的搜尋樹的分支。當節點v的所有邊都已經被探尋過,搜尋將回溯到發現節點v的那條邊的起始節點。
下圖的深度優先遍歷順序為:1->2->4->8->5->3->6->7
BFS(Breadth-First-Search):廣度優先搜尋
從根節點開始,沿著樹(圖)的寬度遍歷樹(圖)的節點,如果所有節點均被訪問,則演算法終止。
下圖的深度優先遍歷順序為:1->2->4->8->5->3->6->7
分別使用棧和佇列實現深度優先搜尋和廣度優先搜尋:
import java.util.ArrayDeque;
public class BinaryTree {
static class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode (int val) {
this.val = val;
}
}
TreeNode root;
public BinaryTree(int[] array) {
root=makeBinaryTreeByArray(array, 1);
}
/*
* 將陣列轉換為一棵二叉樹 採用遞迴的方式轉換
*/
public static TreeNode makeBinaryTreeByArray(int[] array, int index) {
if(index<array.length) {
int val = array[index];
if (val!=0) {
TreeNode t = new TreeNode(val);
array[index]=0;
t.left=makeBinaryTreeByArray(array, index*2);
t.right=makeBinaryTreeByArray(array, index*2+1);
return t;
}
}
return null;
}
/**
* 深度優先遍歷——使用棧實現
*/
public void depthOrderTraversal() {
if(root==null) {
System.out.println("empty tree");
return;
}
ArrayDeque<TreeNode> stack=new ArrayDeque<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode node = stack.pop();
System.out.print(node.val +" ");
if(node.right!=null) {
stack.push(node.right);
}
if(node.left!=null) {
stack.push(node.left);
}
}
System.out.println("\n");
}
/**
* 廣度優先搜尋——使用佇列實現
*/
public void levelOrderTraversal() {
if(root==null) {
System.out.println("\n");
return;
}
ArrayDeque<TreeNode> queue=new ArrayDeque<TreeNode>();
queue.add(root);
while(!queue.isEmpty()) {
TreeNode node = queue.remove();
System.out.print(node.val + " ");
if(node.left!=null) {
queue.add(node.left);
}
if(node.right!=null) {
queue.add(node.right);
}
}
System.out.println("\n");
}
public static void main(String[] args) {
int[] arr={0,13,65,5,97,25,0,37,22,0,4,28,0,0,32,0};
BinaryTree tree=new BinaryTree(arr);
tree.depthOrderTraversal();
tree.levelOrderTraversal();
}
}
求二叉樹的最小深度:
- 當root節點左右孩子都為空時,返回1
- 當root節點左右孩子有一個為空時,返回不為空的孩子節點的深度
- 當root節點左右孩子都不為空時,返回左右孩子較小深度的節點值
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class Solution {
public static int minDepth(TreeNode root) {
if(root==null) return 0;
if(root.left==null && root.right==null) return 1;
if((root.left!=null && root.right==null)) {
return minDepth(root.left)+1;
}
if((root.right!=null && root.left==null)) {
return minDepth(root.right)+1;
}
if(root.left!=null && root.right!=null) {
return Math.min(minDepth(root.left), minDepth(root.right))+1;
}
return 0;
}
public static void main(String args[]) {
TreeNode one = new TreeNode(1);
TreeNode one_left = new TreeNode(1);
TreeNode one_right = new TreeNode(1);
TreeNode one_left_left = new TreeNode(1);
TreeNode one_left_left_left = new TreeNode(1);
one.left = one_left;
one.right = one_right;
one_left.left = one_left_left;
one_right.left = one_left_left_left;
System.out.print(minDepth(one));
}
}
參考文章:
https://www.jianshu.com/p/b086986969e6