LeetCode | Minimum Depth of Binary Tree

题目:给定一个二叉树,找到其最小深度。最小深度是从根节点到最近叶节点的最短路径的节点数。

 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     /**
11     最小深度为:根节点到达最近叶节点的最短路径
12     总体思想是:左子树最小深度+1,右子树最小深度+1,取小的一个
13     需要考虑:当树只有一个子树的情况下,最小深度为:左右子树的最大值
14     */
15     public class Solution {
16         public int run(TreeNode root) {
17             if(root == null)
18                 return 0;
19
20             int left = run(root.left) + 1;
21             int right =run(root.right) + 1 ;
22
23             if(left == 1 || right == 1)
24                 return left > right ? left : right;
25             else
26                 return left > right ? right : left;
27         }
28     }
时间: 2024-08-05 11:11:01

LeetCode | Minimum Depth of Binary Tree的相关文章

[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] Minimum Depth of Binary Tree ,到叶子节点的最小距离 (python)

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. class Solution: # @param root, a tree node # @return an integer minDep =-1 def minDepth(se

leetcode Minimum Depth of Binary Tree C++题解

题目描述 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 Minimum Depth of Binary Tree 找最小深度(返回最小深度)

题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL)

leetcode:Minimum Depth of Binary Tree【Python版】

1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution:

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 -day17 Path Sum I II & Flatten Binary Tree to Linked List & 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】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

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