leetcood学习笔记-111-二叉树的最小深度
生活随笔
收集整理的這篇文章主要介紹了
leetcood学习笔记-111-二叉树的最小深度
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目描述:
第一次提交:
class Solution(object):def minDepth(self, root):""":type root: TreeNode:rtype: int"""if not root:return 0if root.left and root.right:return min(self.minDepth(root.left)+1,self.minDepth(root.right)+1)if not root.left and root.right:return self.minDepth(root.right)+1if not root.right and root.left:return self.minDepth(root.left)+1if not root.left and not root.right:return 1優(yōu)化后:
class Solution(object):def minDepth(self, root):""":type root: TreeNode:rtype: int"""if not root: return 0if not root.left or not root.right:return 1 + max(self.minDepth(root.right), self.minDepth(root.left))else:return 1 + min(self.minDepth(root.right), self.minDepth(root.left))?
轉(zhuǎn)載于:https://www.cnblogs.com/oldby/p/10606640.html
總結(jié)
以上是生活随笔為你收集整理的leetcood学习笔记-111-二叉树的最小深度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡审核久的原因有哪些?信用卡审核久越
- 下一篇: 中信银行信用卡怎么查卡号?这几种方法教你