πDay 3. Diameter of a Binary Tree π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
Given a binary tree, the diameter (or width) is defined as the number of edges on the longest path between two leaf nodes in the tree. This path may or may not pass through the root. Your task is to determine the diameter of the tree.
π Example Walkthrough:
Input:
root[] = [1, 2, 3]
Output:
2
Explanation:
The longest path has 2 edges: (node 2 -> node 1 -> node 3).
Input:
root[] = [5, 8, 6, 3, 7, 9]
Output:
4
Explanation:
The longest path has 4 edges: (node 3 -> node 8 -> node 5 -> node 6 -> node 9).
Constraints
1 β€ number of nodes β€ $10^5$
0 β€ node->data β€ $10^5$
π― My Approach:
Single DFS Traversal (Pair Method): Use a recursive function that returns a pair: the height of the current subtree and the diameter computed so far. For each node, compute:
The height as
1 + max(height(left), height(right))
.The diameter as the maximum of:
The diameter in the left subtree.
The diameter in the right subtree.
The sum of the heights of the left and right subtrees (which represents the path passing through the current node).
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), as each node is visited exactly once.
Expected Auxiliary Space Complexity: O(H), where H is the height of the tree (due to recursion stack).
π Solution Code
Code (C)
struct Pair {
int h, d; // h: height, d: diameter
};
struct Pair f(struct Node* r) {
struct Pair p = {0, 0}; // Base case: empty node returns {0,0}
if (!r) return p;
// Recursively compute left and right subtree results
struct Pair a = f(r->left), b = f(r->right);
p.h = 1 + (a.h > b.h ? a.h : b.h); // Height is max of left/right + 1
int m = a.d > b.d ? a.d : b.d; // m: max diameter from children
int s = a.h + b.h; // s: diameter passing through current node
p.d = m > s ? m : s; // Maximum diameter so far
return p;
}
int diameter(struct Node* root) {
return f(root).d; // Return computed diameter
}
Code (C++)
class Solution {
public:
int dfs(Node* n, int &d) {
if (!n) return 0; // Base case: empty node has height 0
int l = dfs(n->left, d), r = dfs(n->right, d);
d = max(d, l + r); // Update diameter if path through n is larger
return 1 + max(l, r); // Return height of current node
}
int diameter(Node* root) {
int d = 0;
dfs(root, d);
return d; // Final diameter
}
};
Code (Java)
class Solution {
int[] f(Node r) {
if (r == null) return new int[]{0, 0};
int[] a = f(r.left), b = f(r.right);
int h = 1 + Math.max(a[0], b[0]);
int d = Math.max(Math.max(a[1], b[1]), a[0] + b[0]);
return new int[]{h, d};
}
int diameter(Node root) {
return f(root)[1];
}
}
Code (Python)
class Solution:
def f(self, r):
if not r:
return (0, 0)
a, b = self.f(r.left), self.f(r.right)
h = 1 + max(a[0], b[0])
d = max(a[1], b[1], a[0] + b[0])
return (h, d)
def diameter(self, root):
return self.f(root)[1]
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated