543. Diameter of Binary Tree
Tags:
Easy
Skills:
Tree
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Approach
Solution
1function diameterOfBinaryTree(root: TreeNode | null): number {
2 let max_diameter = 0;
3
4 function dfs(node: TreeNode | null): number {
5 if(node === null) {
6 return 0
7 }
8
9 let left_heigh = dfs(node.left);
10 let right_heigh = dfs(node.right)
11
12 max_diameter = Math.max(max_diameter, left_heigh + right_heigh)
13 return Math.max(left_heigh, right_heigh) + 1;
14 }
15
16 dfs(root);
17 return max_diameter
18};