[Leetcode]Maximum Depth of Binary Tree
来源:互联网 发布:r230清零软件图解 编辑:程序博客网 时间:2024/06/06 02:30
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
这题要返回树的最大深度~ 递归解法很简单
class Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): if root is None: return 0 return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
还有一种非递归解法,采用层序遍历
class Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): if root is None: return 0 depth = 1 queue = [root, None] while len(queue) > 1: node = queue.pop(0) if node is None: depth += 1 queue.append(None) continue if node.left: queue.append(node.left) if node.right: queue.append(node.right) return depth
0 0
- Leetcode - Tree - Maximum Depth of Binary Tree
- [leetcode][tree] Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- [Leetcode] Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- Leetcode 104 Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- Leetcode: Maximum Depth of Binary Tree
- LeetCode-Maximum Depth of Binary Tree
- [leetcode] Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- LeetCode | Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- java 8种基本数据类型及封装类及默认值
- C指针使用注意事项
- C/C++ 变量声明和定义的区别 静态变量的作用
- 自适应阈值二值化
- 微信分享keystore问题
- [Leetcode]Maximum Depth of Binary Tree
- 【转载】如何使用Git上传项目代码到github
- 第八章 排序技术
- Android中WindowManager详解
- [Flask]学习杂记--模板
- 在Eclipse添加Android兼容包( v4、v7 appcompat )
- visual studio 2005 ASP.net 断点调试方法
- 最大的子序列和问题
- Android 获取assets绝对路径