You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/g0101_0200/s0104_maximum_depth_of_binary_tree/readme.md
+9-18
Original file line number
Diff line number
Diff line change
@@ -37,39 +37,30 @@ A binary tree's **maximum depth** is the number of nodes along the longest path
37
37
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
38
38
*`-100 <= Node.val <= 100`
39
39
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:
41
41
42
42
1.**Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
43
43
44
44
2.**Create a `maxDepth` method**: This method takes the root node of the binary tree as input and returns its maximum depth.
45
45
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.
47
47
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.
52
49
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.
54
51
55
52
Here's the Java implementation:
56
53
57
54
```java
58
55
classSolution {
59
56
publicintmaxDepth(TreeNoderoot) {
60
57
if (root ==null) return0; // 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
+
returnMath.max(leftDepth, rightDepth) +1; // Return maximum depth of left and right subtrees, plus 1 for the current node
62
61
}
63
62
64
-
// Helper method to recursively calculate maximum depth
65
-
privateintdfs(TreeNodenode) {
66
-
if (node ==null) return0;
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
-
returnMath.max(leftDepth, rightDepth) +1; // Return maximum depth plus 1 for current node
70
-
}
71
-
72
-
// TreeNode definition
63
+
// Definition for a TreeNode
73
64
publicclassTreeNode {
74
65
int val;
75
66
TreeNode left;
@@ -86,4 +77,4 @@ class Solution {
86
77
}
87
78
```
88
79
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