Leetcode111——二叉树的最小深度
程序员文章站
2022-06-19 12:08:41
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...
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
本文地址:https://blog.csdn.net/aaqian1/article/details/112002613
上一篇: spring依赖,注解,配置以及actuator和profile粗浅使用
下一篇: 线程初学