Skip to content

Commit 1d1b96d

Browse files
committed
Improved 104
1 parent 0bbac78 commit 1d1b96d

File tree

1 file changed

+9
-18
lines changed
  • src/main/java/g0101_0200/s0104_maximum_depth_of_binary_tree

1 file changed

+9
-18
lines changed

src/main/java/g0101_0200/s0104_maximum_depth_of_binary_tree/readme.md

+9-18
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,30 @@ A binary tree's **maximum depth** is the number of nodes along the longest path
3737
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
3838
* `-100 <= Node.val <= 100`
3939

40-
To solve this problem in Java with a `Solution` class, we'll use a depth-first search (DFS) traversal approach. Below are the steps:
40+
To solve the "Maximum Depth of Binary Tree" problem in Java with a `Solution` class, we'll perform a depth-first search (DFS) traversal of the binary tree. Below are the steps:
4141

4242
1. **Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
4343

4444
2. **Create a `maxDepth` method**: This method takes the root node of the binary tree as input and returns its maximum depth.
4545

46-
3. **Check for null root**: Check if the root is null. If so, return 0, as an empty tree has a maximum depth of 0.
46+
3. **Check for null root**: Check if the root is null. If it is, return 0 as the depth.
4747

48-
4. **Recursively calculate the maximum depth**: Define a helper method `dfs` to recursively calculate the maximum depth of the binary tree.
49-
- If the current node is null, return 0.
50-
- Otherwise, recursively calculate the maximum depth of the left and right subtrees.
51-
- Return the maximum depth of the left and right subtrees, plus 1 (to account for the current node).
48+
4. **Perform DFS traversal**: Recursively compute the depth of the left and right subtrees. The maximum depth of the binary tree is the maximum depth of its left and right subtrees, plus 1 for the current node.
5249

53-
5. **Return the maximum depth**: After traversing the entire tree, return the maximum depth calculated.
50+
5. **Return the result**: After the DFS traversal is complete, return the maximum depth of the binary tree.
5451

5552
Here's the Java implementation:
5653

5754
```java
5855
class Solution {
5956
public int maxDepth(TreeNode root) {
6057
if (root == null) return 0; // Check for empty tree
61-
return dfs(root); // Recursively calculate maximum depth
58+
int leftDepth = maxDepth(root.left); // Compute depth of left subtree
59+
int rightDepth = maxDepth(root.right); // Compute depth of right subtree
60+
return Math.max(leftDepth, rightDepth) + 1; // Return maximum depth of left and right subtrees, plus 1 for the current node
6261
}
6362

64-
// Helper method to recursively calculate maximum depth
65-
private int dfs(TreeNode node) {
66-
if (node == null) return 0;
67-
int leftDepth = dfs(node.left); // Calculate maximum depth of left subtree
68-
int rightDepth = dfs(node.right); // Calculate maximum depth of right subtree
69-
return Math.max(leftDepth, rightDepth) + 1; // Return maximum depth plus 1 for current node
70-
}
71-
72-
// TreeNode definition
63+
// Definition for a TreeNode
7364
public class TreeNode {
7465
int val;
7566
TreeNode left;
@@ -86,4 +77,4 @@ class Solution {
8677
}
8778
```
8879

89-
This implementation follows the steps outlined above and efficiently solves the problem of finding the maximum depth of a binary tree in Java.
80+
This implementation follows the steps outlined above and efficiently computes the maximum depth of the binary tree in Java using DFS traversal.

0 commit comments

Comments
 (0)