-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC104_MaximumDepthOfBinaryTree.java
More file actions
64 lines (51 loc) · 1.67 KB
/
LC104_MaximumDepthOfBinaryTree.java
File metadata and controls
64 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package practise.leetCode;
import java.util.LinkedList;
import java.util.Queue;
public class LC104_MaximumDepthOfBinaryTree {
// Definition for a binary tree node.
// static class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
//
// TreeNode(int val) {
// this.val = val;
// }
// }
public static void main(String[] args) {
// Input: [3,9,20,null,null,15,7]
Integer[] arr = {3, 9, 20, null, null, 15, 7};
TreeNode root = buildTree(arr);
LC104_MaximumDepthOfBinaryTree obj =
new LC104_MaximumDepthOfBinaryTree();
int result = obj.maxDepth(root);
System.out.println("Output: " + result); // Expected: 3
}
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
// Helper method to build tree from level-order array
private static TreeNode buildTree(Integer[] arr) {
if (arr == null || arr.length == 0 || arr[0] == null) return null;
TreeNode root = new TreeNode(arr[0]);
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int i = 1;
while (i < arr.length) {
TreeNode current = queue.poll();
if (arr[i] != null) {
current.left = new TreeNode(arr[i]);
queue.offer(current.left);
}
i++;
if (i < arr.length && arr[i] != null) {
current.right = new TreeNode(arr[i]);
queue.offer(current.right);
}
i++;
}
return root;
}
}