Maximum Depth of Binary Tree
来源:互联网 发布:mac 定时关闭程序 编辑:程序博客网 时间:2024/05/18 12:32
leetcode第104题,最最大树高,由于标签是深度搜索,我就按照深度搜索先做一遍,这要求求最大深度,设置一个变量记录一下深度就可以了,不过这个深度在什么位置自增需要注意,否则结果可能会多1或者少1.最终的递归出口就是节点没有子节点,也就是该节点为叶子节点的时候。由于每个节点接收到的都是来自左右两边子树的深度,因此需要取最大值在继续向上层递交。
class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ d = 0 def dfs(root,d): if root == None: return d d1 = dfs(root.right,d+1) d2 = dfs(root.left,d+1) return max(d1,d2) d = dfs(root,d) return d
0 0
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- 一个新手与老手的差距
- ubuntu 进不去界面解决方案
- Servlet中的过滤器Filter详解
- 开发日记——手机安全卫士 Day06 2016-7-25 修改部分界面,增加了联系人搜索功能,完成部分黑名单功能
- 算法:枚举(穷举)法
- Maximum Depth of Binary Tree
- Data frame columns as arguments to dplyr functions
- word 的使用(七) —— 绘图工具
- AutoCompleteTextView和MultiAutoCompleteTextView
- system.sys 与 system.err 打印的区别异同
- 各种细的知识点(一)
- 手机端QQ登录openid与网站端QQ登录openid不一样
- CentOS7修改yum阿里源
- 判断一个序列是否为另一个栈的出栈序列(栈的压入、弹出序列)