Minimum Depth of Binary Tree
来源:互联网 发布:伊万卡 知乎 编辑:程序博客网 时间:2024/05/16 11:03
leetcode第111题,简单的二叉树深搜。
搜索时注意只有叶子节点才具有深度,因此当搜索到叶子节点的时候就应该作为递归的出口条件来返回深度了,另外,根据左右两棵子树的深度,找出最小值继续向上递交即可。
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def dfs(self,node,depth): if node.left == None and node.right == None: return depth d1 = d2 = 9999 if node.left != None: d1 = self.dfs(node.left,depth+1) if node.right != None: d2 = self.dfs(node.right,depth+1) return min(d1,d2) def minDepth(self, root): if root == None: return 0 return self.dfs(root,1)
0 0
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- C#中HashTable的定义与使用方法
- 解析PSNR和SSIM
- 笔试总结--DNS(域名系统)
- APK反编译
- 概率论之全概率公式
- Minimum Depth of Binary Tree
- js微信分享到朋友圈 分享给朋友代码
- 备战软考,重在行动
- jvm历史
- 中文 Appium API 文档
- luck number 幸运数
- 电脑摄像头阅卷系统
- 单例模式的反射相关问题
- 171. Excel Sheet Column Number