[LeetCode][Python3] 543. Diameter of Binary Tree
2018. 10. 9. 03:24 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/diameter-of-binary-tree/description/
My Solution :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.diameter = 1
self.find_depth(root)
return self.diameter - 1
def find_depth(self, root):
if not root:
return 0
left = self.find_depth(root.left)
right = self.find_depth(root.right)
self.diameter = max(self.diameter, 1 + left + right)
return 1 + max(left, right)
Comment :
이 문제는 좋은 방법이 떠오르지 않아 discuss를 참조하였다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 88. Merge Sorted Array (0) | 2018.11.03 |
---|---|
[LeetCode][Python3] 118. Pascal's Triangle (0) | 2018.10.17 |
[LeetCode][Python3] 125. Valid Palindrome (0) | 2018.10.17 |
[LeetCode][Python3] 66. Plus One (0) | 2018.10.15 |
[LeetCode][Python3] 617. Merge Two Binary Trees (0) | 2018.10.08 |
[LeetCode][Python3] 581. Shortest Unsorted Continuous Subarray (0) | 2018.10.08 |
[LeetCode][Python3] 572. Subtree of Another Tree (0) | 2018.10.07 |
[LeetCode][Python3] 538. Convert BST to Greater Tree (0) | 2018.10.07 |
최근에 달린 댓글 최근에 달린 댓글