-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathDiameter Binarytree_Leetcode 543
45 lines (41 loc) · 1.18 KB
/
Diameter Binarytree_Leetcode 543
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Node:
def __init__(self, item):
self.data = item
self.left = None
self.right = None
class Solution(object):
def diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
lheight = self.height_tree(root.left)
rheight = self.height_tree(root.right)
ldiam = self.diameterOfBinaryTree(root.left)
rdiam = self.diameterOfBinaryTree(root.right)
a = max(lheight + rheight, max(ldiam, rdiam))
return a
def height_tree(self, root):
if root == None:
return 0
else:
ldepth = self.height_tree(root.left)
rdepth = self.height_tree(root.right)
if ldepth > rdepth:
return ldepth + 1
else:
return rdepth + 1
root = Node(9)
root.left = Node(1)
root.right = Node(10)
root.left.left = Node(2)
root.left.right = Node(3)
root.left.left.left = Node(4)
root.left.left.right = Node(5)
root.left.right.left = Node(6)
root.left.right.right = Node(7)
root.left.right.left.left = Node(8)
s=Solution()
print(s.diameterOfBinaryTree(root))