Skip to content

Commit

Permalink
Diameter of Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed Sep 6, 2024
1 parent 70afefa commit 8e4ed22
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
| 0347 | Medium | Top K Frequent Elements | Array, HashMap, Bucket Sort | [Solution](./docs/0347-Top-K-Frequent-Elements.md) |
| 0392 | Easy | Is Subsequence | Two Pointers, String, Dynamic Programming | [solution](./docs/0392-Is-Subsequence.md) |
| 0424 | Medium | Longest Repeating Character Replacement | HashTable, String, Sliding Windows | [solution](./docs/0424-Longest-Repeating-Character-Replacement.md) |
| 0543 | Easy | Diameter of Binary Tree | Tree, Depth-First Search, Binary Tree | [solution](./docs/0543-Diameter-of-Binary-Tree.md) |
| 0567 | Medium | Permutation in String | Hash Table, Two Pointers, String, Sliding Window | [solution](./docs/0567-Permutation-In-String.md) |
| 0704 | Easy | Binary Search | Array, Binary Search | [solution](./docs/0704-Binary-Search.md) |
| 0739 | Medium | Daily Temperatures | Array, Stack, Monotonic Stack | [solution](./docs/0139-Daily-Temperatures.md) |
Expand Down
40 changes: 40 additions & 0 deletions docs/0543-Diameter-of-Binary-Tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/)

# Intuition

The diameter of a binary tree is the longest path between any two nodes, measured by the number of edges. This path can
pass through or exclude the root. To find the diameter, we need to examine the sum of the heights of the left and right
subtrees at each node, as this provides the longest path that passes through that node.

## Approach

1. **Recursive Height Calculation with Diameter Update:**
- Use a helper function `height` to recursively calculate the height of each subtree.
- For each node, calculate the height of its left and right subtrees.
- The potential diameter at each node is the sum of the heights of its left and right subtrees (
`leftHeight + rightHeight`). Update the global diameter if this value is larger than the current maximum diameter.
- Return the height of the current node, which is `1 + max(leftHeight, rightHeight)`.
2. **Global Diameter Tracking:**
- Use an array diameter with a single element to keep track of the maximum diameter found so far (since Java
does not allow primitive integers to be modified within recursive calls).
- Start the recursive height calculation from the root, which will update the diameter as it traverses each node.
3. **Return the Result:** After traversing the tree, `diameter[0]` will contain the maximum diameter, which is the
result.

## Complexity

- **Time Complexity: `O(n)`**, where `n` is the number of nodes in the tree. Each node is visited once to calculate its
height and update the diameter.
- **Space Complexity: `O(h)`**, where `h` is the height of the tree. This represents the space used by the recursive
call stack. In the worst case (skewed tree),`h = n`, and in the best case (balanced tree), `h = log(n)`.

## Code

- [Java](../src/main/java/io/dksifoua/leetcode/diameterofbinarytree/Solution.java)

## Summary

This solution calculates the diameter of a binary tree by recursively determining the height of each subtree while
updating the diameter at each node. By calculating `leftHeight + rightHeight` at each node, we capture the longest path
passing through that node, ensuring the correct diameter is found. The approach is efficient with `O(n)` time
complexity, as it leverages a single traversal to determine both height and diameter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.dksifoua.leetcode.diameterofbinarytree;

import io.dksifoua.leetcode.utils.TreeNode;

public class Solution {

private int height(TreeNode node, int[] diameter) {
if (node == null) {
return 0;
}

int leftHeight = this.height(node.getLeft(), diameter);
int rightHeight = this.height(node.getRight(), diameter);

diameter[0] = Math.max(diameter[0], leftHeight + rightHeight);

return 1 + Math.max(leftHeight, rightHeight);
}

public int diameterOfBinaryTree(TreeNode root) {
int[] diameter = new int[1];

this.height(root, diameter);

return diameter[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.dksifoua.leetcode.diameterofbinarytree;

import io.dksifoua.leetcode.utils.TreeNode;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SolutionTest {

final Solution solution = new Solution();

@Test
void test1() {
TreeNode root = TreeNode.build(new Integer[] { 1, 2, 3, 4, 5, null, null });
assertEquals(3, solution.diameterOfBinaryTree(root));
}

@Test
void test2() {
TreeNode root = TreeNode.build(new Integer[] { 1, 2, null });
assertEquals(1, solution.diameterOfBinaryTree(root));
}
}

0 comments on commit 8e4ed22

Please sign in to comment.