Minimum Depth of Binary Tree - LeetCode

来源:互联网 发布:好用的拼图软件 编辑:程序博客网 时间:2024/05/17 04:46

Minimum Depth of Binary Tree - LeetCode

题目:

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.


分析:

感觉应该是有关树的题目都可以递归解决。找到最小路径用min函数。

代码:

class Solution:    # @param root, a tree node    # @return an integer    def minDepth(self, root):        if not root:            return 0        if not root.right:            return self.minDepth(root.left)+1        elif not root.left:            return self.minDepth(root.right)+1        else:            return min(self.minDepth(root.right),self.minDepth(root.left))+1


0 0
原创粉丝点击