leetcode-543 二叉树的直径 Python
2020-03-15 12:14
411 查看
方法一:
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: self.ans = 1 def depth(node): if not node: return 0 L = depth(node.left) R = depth(node.right) self.ans = max(self.ans, L+R+1) return max(L, R)+1 depth(root) return self.ans-1
方法二:
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: self.max = 0 self.depth(root) return self.max def depth(self.root): if not root: return 0 l = self.depth(root.left) r = self.depth(root.right) self.max = max(self.max, l+r) return max(l, r) + 1
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Leetcode 543. 二叉树的直径-----python
- leetcode--对称二叉树--python
- Leetcode 102 二叉树的层次遍历 Python
- 用python写leetcode【14】 -- 二叉树中的最大路径和(124)
- LeetCode--102. Binary Tree Level Order Traversal(二叉树水平遍历)Python
- 利用python 完成 leetcode 199 二叉树的右视图
- LeetCode700 二叉树中的搜索 python3
- Leetcode 104 python 二叉树的最大深度
- leetcode 第105题(从前序与中序遍历序列构造二叉树) ,第106题(从中序与后序遍历序列构造二叉树)python解法(用时40ms)
- 利用python 完成leetcode111 二叉树的最小深度
- LeetCode 331. Verify Preorder Serialization of a Binary Treet 二叉树前序遍历验证 Python Solution
- LeetCode 543. Diameter of Binary Tree (二叉树的直径)
- LeetCode 105,106. Construct Binary Tree 重建二叉树 Python Solution
- LeetCode 144. 二叉树的前序遍历 Python
- LeetCode 623 在二叉树中增加一行 Python
- Leetcode 144 python 二叉树的前序遍历
- Leetcode 145 python 二叉树的后序遍历
- leetcode107. 二叉树的层次遍历 II(python实现)
- leetcode in python 543
- 利用python 完成leetcode 114 二叉树展开为链表