日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

發布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【LeetCode】Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

遞歸和非遞歸,此提比較簡單。廣度優先遍歷即可。關鍵之處就在于如何保持訪問深度。

下面是4種代碼:

?

1 import java.util.ArrayList; 2 import java.util.LinkedList; 3 import java.util.List; 4 import java.util.Queue; 5 6 class TreeNode { 7 int val; 8 TreeNode left; 9 TreeNode right; 10 11 TreeNode(int x) { 12 val = x; 13 } 14 } 15 public class MinimumDepthofBinaryTree { 16 /** 17 * 遞歸深度遍歷 18 * @param root 19 * @return 20 */ 21 public int minDepth1(TreeNode root) { 22 if(root==null) 23 return 0; 24 if(root.left==null&&root.right==null){ 25 return 1; 26 } 27 int left=minDepth1(root.left); 28 int right=minDepth1(root.right); 29 if(left==0){ 30 return right+1; 31 } 32 if(right==0){ 33 return left+1; 34 } 35 else{ 36 return Math.min(right, left)+1; 37 } 38 } 39 public int minDepth2(TreeNode root) { 40 if(root==null) 41 return 0; 42 int left=minDepth1(root.left); 43 int right=minDepth1(root.right); 44 45 if(left==0&&right==0){ 46 return 1; 47 } 48 if(left==0){ 49 right= Integer.MAX_VALUE; 50 } 51 if(right==0){ 52 left=Integer.MAX_VALUE; 53 } 54 55 return Math.min(right, left)+1; 56 57 } 58 59 /** 60 * 廣度優先搜索。一旦發現葉子結點,返回遍歷深度。 61 * @param root 62 * @return 63 */ 64 public int minDepth3(TreeNode root) { 65 if(root==null){ 66 return 0; 67 } 68 Queue<TreeNode>queue=new LinkedList<>(); 69 queue.add(root); 70 int count=queue.size();//用來保存訪問當前層次剩余未訪問的節點。 71 int depth=1;//用來保存二叉樹的訪問深度 72 while(!queue.isEmpty()){ 73 //廣度遍歷 74 TreeNode topNode=queue.poll(); 75 count--; 76 if(topNode.left!=null){ 77 queue.add(topNode.left); 78 } 79 if(topNode.right!=null){ 80 queue.add(topNode.right); 81 } 82 //發現葉子節點 83 if(topNode.left==null&&topNode.right==null){ 84 return depth; 85 } 86 //訪問一層完畢 87 if(count==0){ 88 depth++; 89 count=queue.size(); 90 } 91 92 } 93 return 0; 94 } 95 /** 96 * 廣度優先搜索。一旦發現葉子結點,返回遍歷深度。 97 * @param root 98 * @return 99 */ 100 public int minDepth4(TreeNode root) { 101 if(root==null){ 102 return 0; 103 } 104 List<TreeNode>list=new ArrayList<>(); 105 int count=1; 106 list.add(root); 107 while(list.size()!=0){ 108 List<TreeNode>singleList=new ArrayList<>(); 109 for(TreeNode t:list){ 110 if(t.left!=null){ 111 singleList.add(t.left); 112 }if(t.right!=null){ 113 singleList.add(t.right); 114 } 115 if(t.left==null&&t.right==null){ 116 return count; 117 } 118 } 119 count++; 120 list=singleList; 121 } 122 return 0; 123 } 124 public static void main(String[] args) { 125 TreeNode rootNode1 = new TreeNode(1); 126 TreeNode rootNode2 = new TreeNode(2); 127 TreeNode rootNode3 = new TreeNode(3); 128 TreeNode rootNode4 = new TreeNode(4); 129 TreeNode rootNode5 = new TreeNode(5); 130 TreeNode rootNode6 = new TreeNode(6); 131 TreeNode rootNode7 = new TreeNode(7); 132 rootNode1.left = rootNode2; 133 rootNode1.right = rootNode3; 134 rootNode2.left = rootNode4; 135 rootNode2.right = rootNode5; 136 rootNode3.left = rootNode6; 137 rootNode3.right = rootNode7; 138 MinimumDepthofBinaryTree minimumDepthofBinaryTree=new MinimumDepthofBinaryTree(); 139 System.out.println(minimumDepthofBinaryTree.minDepth4(rootNode1)); 140 141 } 142 143 }

?

轉載于:https://www.cnblogs.com/hitkb/p/4243664.html

總結

以上是生活随笔為你收集整理的【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。