LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
来源:互联网 发布:百度网盘mac版怎么安装 编辑:程序博客网 时间:2024/05/16 09:17
题目:
给定一个二叉树,返回这个二叉树的最大深度
解题思路:
使用递归,保存所有叶子节点的深度。选择这些深度中的最大值,则为该二叉树的最大深度。
代码(Python):
# 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 maxDepth(self, root): """ :type root: TreeNode :rtype: int """ List = [] if root==None: return 0 def readTree(root,count): if root.left==None and root.right==None: count = count+1 List.append(count) return elif root.left==None and root.right!=None: count = count+1 readTree(root.right,count) elif root.left!=None and root.right==None: count = count+1 readTree(root.left,count) else: readTree(root.left,count+1) readTree(root.right,count+1) readTree(root,0) print List return max(List)
阅读全文
0 0
- LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- LeetCode OJ 之 Maximum Depth of Binary Tree (二叉树的最大深度)
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- leetcode系列(28)Maximum Depth of Binary Tree 求二叉树的最大深度
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- Leetcode 104.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(二叉树的最大深度)
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode-104 Maximum Depth of Binary Tree 二叉树的最大深度
- [LeetCode]104. Maximum Depth of Binary Tree--二叉树的最大深度
- Leetcode#104. Maximum Depth of Binary Tree(最大二叉树的深度)
- kubeadm1.8.4安装kubernetes1.8.4集群
- [测试]Linux查看和结束进程命令详解
- 倒三角形(triangle)
- poj 3468 A Simple Problem with Integers(线段树+树状数组区间更新区间查询)
- BZOJ1027 [HNOI2004]打鼹鼠 【dp】
- LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
- 简单修复360安全检测提示的发现robots文件漏洞
- pig 笔记
- 卡巴斯基发布安全公告:2018年威胁预测
- Python 条件和循环
- redis位图法统计活跃用户
- nginx 禁止访问.svn的目录
- Asp.net 如何实现微信公众号授权登录
- VINS理论与代码详解1——框架解析