File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ class Node:
2+ def __init__(self, item):
3+ self.data = item
4+ self.left = None
5+ self.right = None
6+
7+
8+ class Solution(object):
9+ def diameterOfBinaryTree(self, root):
10+ """
11+ :type root: TreeNode
12+ :rtype: int
13+ """
14+ if root == None:
15+ return 0
16+ lheight = self.height_tree(root.left)
17+ rheight = self.height_tree(root.right)
18+ ldiam = self.diameterOfBinaryTree(root.left)
19+ rdiam = self.diameterOfBinaryTree(root.right)
20+ a = max(lheight + rheight, max(ldiam, rdiam))
21+ return a
22+
23+ def height_tree(self, root):
24+ if root == None:
25+ return 0
26+ else:
27+ ldepth = self.height_tree(root.left)
28+ rdepth = self.height_tree(root.right)
29+ if ldepth > rdepth:
30+ return ldepth + 1
31+ else:
32+ return rdepth + 1
33+ root = Node(9)
34+ root.left = Node(1)
35+ root.right = Node(10)
36+ root.left.left = Node(2)
37+ root.left.right = Node(3)
38+ root.left.left.left = Node(4)
39+ root.left.left.right = Node(5)
40+ root.left.right.left = Node(6)
41+ root.left.right.right = Node(7)
42+ root.left.right.left.left = Node(8)
43+
44+ s=Solution()
45+ print(s.diameterOfBinaryTree(root))
You can’t perform that action at this time.
0 commit comments