Minimum -depth -of -binary [email protected]

链接:https://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724?tpId=46&tqId=29030&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking
来源:牛客网

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.

基本思想:

  叶子节点:度0的结点,没有孩子。

  二叉树的最小深度,不同于二叉树的最大深度,不能够单纯使用递归,方式比较所有的叶子节点同时记录所有的到根节点的深度,最后进行比较。

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int run(TreeNode root) {
12         if (root==null) return 0;
13         /*使用递归判断此节点是否是叶子节点*/
14         if(root.left==null && root.right==null) return 1;
15            /*二叉树左子树空,叶子节点在右子树上*/
16         if(root.left==null)
17             return run(root.right)+1;
18         if(root.right==null)
19             return 1+run(root.left);
20         /*左右儿子,比较得到其中最小值*/
21         return Math.min(run(root.left),run(root.right))+1;
22     }
23 }

C++语言更加的简洁,求最大深度树,将最后11行代码修改成   max(L,R)+1;

 1 class Solution {
 2 public:
 3     int run(TreeNode *root) {
 4         if(root==NULL)
 5             return 0;
 6         int L=run(root->left);
 7         int R=run(root->right);
 8
 9         if(L==0 || R==0)
10             return 1+L+R;/*此节点不存在,父节点最多只有一个儿子,计算出叶子叶子节点到此结点的深度,要么只有一个节点,要么根节点*/
11         return (L<R?L:R)+1;
12
13     }
14 };
时间: 2024-10-05 03:18:26

Minimum -depth -of -binary [email protected]的相关文章

35: Minimum Depth of Binary Tree

/************************************************************************/        /*       35:     Minimum Depth of Binary Tree                                         */        /***********************************************************************

Minimum Depth of Binary Tree leetcode java

题目: 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. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/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. 解题思路:分几种情况考虑:1,树为空,

Leetcode 111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree Total Accepted: 114343 Total Submissions:368035 Difficulty: Easy 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 le

LeetCode My Solution: Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions 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.

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

[Leetcode][JAVA] Minimum Depth of Binary Tree &amp;&amp; Balanced Binary Tree

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. 比较左子树和右子树的深度,取小的那个返回上来,并+1. 需要注意的是如果没有左子树或右子树.那么无条件取存在的那一边子树深

【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

33. Minimum Depth of Binary Tree &amp;&amp; Balanced Binary Tree

Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/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