class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
if root.left is None:
return 1 + self.minDepth(root.right)
if root.right is None:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
用
java
写的,楼主看看https://github.com/terry83299387/MyTest/blob/master/BinaryTreeMinSum.java
我觉得是简单dp(瞎说的
动态规划中的入门问题。
用php试了下,可以看看https://github.com/chianquan/Mytest/blob/master/shortest.php
应该是树形DP吧
leetcode上有类似的,不过只是求跳数的题目:https://leetcode.com/problems/minimum-depth-of-binary-tree/
这是我的
python
实现,你稍微改一下就行