[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(self, root):
        if root is None:
            return 0
        self.helper(root,1)
        return self.minDep

    # @param dep, 当前为第几层
    def helper(self,root,dep):
        if root.left is None and root.right is None:
            if self.minDep is -1:
                self.minDep =dep
            elif self.minDep >dep:
                self.minDep =dep

        if dep >= self.minDep and self.minDep is not -1:
            return False

        if root.left is not None:
            self.helper(root.left,dep+1)

        if root.right is not None:
            self.helper(root.right,dep+1)

这题我使用的是DFS,但是我设置了一个minDep的global变量。这个变量储存目前最小的【叶子节点到根的距离】,所以其他分支遍历的时候,检查一下dep(当前距离)是否大于minDep,如果大于或者等于,就结束该分支的遍历

时间: 2025-01-11 10:33:37

[leetcode] Minimum Depth of Binary Tree ,到叶子节点的最小距离 (python)的相关文章

[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 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

题目:给定一个二叉树,找到其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数. 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 总体思想是:左子树

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:

【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

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 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 & 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