1+ """
2+ LeetCode 543. Diameter of Binary Tree
3+
4+ Problem:
5+ Given the root of a binary tree, return the length of the
6+ diameter of the tree.
7+
8+ The diameter of a binary tree is the length of the longest path
9+ between any two nodes in a tree. This path may or may not pass
10+ through the root.
11+
12+ The length of a path between two nodes is represented by the
13+ number of edges between them.
14+
15+ Example:
16+
17+ 1
18+ / \
19+ 2 3
20+ / \
21+ 4 5
22+
23+ Output: 3
24+
25+ Explanation:
26+ The longest path is:
27+
28+ 4 -> 2 -> 1 -> 3
29+
30+ Number of edges = 3
31+
32+ ---------------------------------------------------------
33+ Approach: DFS + Height Calculation
34+ ---------------------------------------------------------
35+
36+ Observation:
37+
38+ For every node:
39+
40+ Diameter Through Current Node
41+ =
42+ Height of Left Subtree + Height of Right Subtree
43+
44+ Example:
45+
46+ 1
47+ / \
48+ 2 3
49+
50+ left height = 1
51+ right height = 1
52+
53+ diameter through node 1
54+ = 1 + 1
55+ = 2
56+
57+ While computing heights using DFS, we update the maximum
58+ diameter seen so far.
59+
60+ ---------------------------------------------------------
61+ Algorithm
62+ ---------------------------------------------------------
63+
64+ 1. Create a variable 'diameter' to store the maximum answer.
65+ 2. Define a helper function height(node).
66+ 3. If node is None, return 0.
67+ 4. Recursively calculate:
68+ left_height
69+ right_height
70+ 5. Update:
71+ diameter = max(diameter,
72+ left_height + right_height)
73+ 6. Return:
74+ 1 + max(left_height, right_height)
75+ 7. Start DFS from root.
76+ 8. Return diameter.
77+
78+ ---------------------------------------------------------
79+ Dry Run
80+ ---------------------------------------------------------
81+
82+ Input:
83+
84+ 1
85+ / \
86+ 2 3
87+ / \
88+ 4 5
89+
90+ Node 4:
91+ height = 1
92+
93+ Node 5:
94+ height = 1
95+
96+ Node 2:
97+ left = 1
98+ right = 1
99+
100+ diameter = 1 + 1 = 2
101+
102+ height(2) = 2
103+
104+ Node 3:
105+ height = 1
106+
107+ Node 1:
108+ left = 2
109+ right = 1
110+
111+ diameter = max(2, 2 + 1)
112+ = 3
113+
114+ Answer = 3
115+
116+ ---------------------------------------------------------
117+ Time Complexity
118+ ---------------------------------------------------------
119+
120+ O(n)
121+
122+ Each node is visited exactly once.
123+
124+ ---------------------------------------------------------
125+ Space Complexity
126+ ---------------------------------------------------------
127+
128+ O(h)
129+
130+ h = height of the tree
131+
132+ Recursion stack stores at most h calls.
133+
134+ Worst Case (Skewed Tree):
135+ O(n)
136+
137+ Balanced Tree:
138+ O(log n)
139+
140+ ---------------------------------------------------------
141+ """
142+
143+ # Definition for a binary tree node.
144+ # class TreeNode:
145+ # def __init__(self, val=0, left=None, right=None):
146+ # self.val = val
147+ # self.left = left
148+ # self.right = right
149+
150+ class Solution :
151+ def diameterOfBinaryTree (self , root : Optional [TreeNode ]) -> int :
152+
153+ diameter = 0
154+
155+ def height (node ):
156+ nonlocal diameter
157+
158+ if not node :
159+ return 0
160+
161+ left = height (node .left )
162+ right = height (node .right )
163+
164+ diameter = max (diameter , left + right )
165+
166+ return 1 + max (left , right )
167+
168+ height (root )
169+
170+ return diameter
0 commit comments