-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path543_DiameterOfBinaryTree.py
111 lines (89 loc) · 3.34 KB
/
543_DiameterOfBinaryTree.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# By Will Shin
#
#-------------------------------------------------------------------------------
# LeetCode prompt
#-------------------------------------------------------------------------------
"""
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1
/ \
2 3
/ \
4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
"""
# #
#-------------------------------------------------------------------------------
# Approach
#-------------------------------------------------------------------------------
"""
1. The way that we can approach this is this. For each node, we can calculate that the diameter at each node is the number of nodes in left subtree-path + right subtree-path + 1(the current node)
2.
self.ans = 0
def depth(root):
if not root: return 0
left = depth(root.left)
right = depth(root.right)
# path
self.ans = max(self.ans, left + right)
# depth
return max(left, right) + 1
depth(root)
return self.ans
"""
#-------------------------------------------------------------------------------
# Solution
#-------------------------------------------------------------------------------
#def _mydiameterOfBinaryTree_helper(node):
# if node is None:
# return 0
global ans
ans = 0
def mydiameterOfBinaryTree(node):
if node is None:
return 0
to_return_left = mydiameterOfBinaryTree(node.left)
to_return_right = mydiameterOfBinaryTree(node.right)
global ans
ans = max(ans, to_return_left + to_return_right)
return(max(to_return_left, to_return_right) + 1)
#-------------------------------------------------------------------------------
# Main Leetcode Input Driver
#-------------------------------------------------------------------------------
# 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):
global ans
mydiameterOfBinaryTree(root)
return ans
#-------------------------------------------------------------------------------
# Unit Test
#-------------------------------------------------------------------------------
import unittest
from mymodules import BinaryTree as bt
class TestSolution(unittest.TestCase):
def test_solution(self):
test_tree = bt.Tree()
test_tree.insert(10)
test_tree.insert(5)
test_tree.insert(1)
test_tree.insert(6)
test_tree.insert(15)
test_tree.insert(11)
test_tree.insert(18)
test_tree.printtree(test_tree.root)
self.assertEquals(Solution().diameterOfBinaryTree(test_tree.root), 4)
def test_empty(self):
self.assertEquals(Solution().diameterOfBinaryTree(bt.Tree().root), 0)
unittest.main()