-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumDepthOfBinaryTree.java
More file actions
154 lines (124 loc) · 4.97 KB
/
MaximumDepthOfBinaryTree.java
File metadata and controls
154 lines (124 loc) · 4.97 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package Algorithms.BinaryTrees;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* In Tree, maxDepth and maxHeight are same
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 19 Jan 2025
* @link 104. Maximum Depth of Binary Tree <a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">LeetCode Link</a>
* @topics Tree, Binary Tree, DFS, BFS, Recursion
3
/ \
9 20
/ \
15 7
*/
public class MaximumDepthOfBinaryTree {
public static class TreeNode {int val;TreeNode left, right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
System.out.println("maxDepth using DFS: " + maxDepthUsingDfs(root));
System.out.println("maxDepth using Recursion: " + maxDepthUsingRecursion(root));
System.out.println("maxDepth using Recursion2: " + maxDepthUsingRecursion2(root));
System.out.println("maxDepth using BFS Queue: " + maxDepthUsingBfsQueue(root));
System.out.println("maxDepth using Stack: " + maxDepthUsingStack(root));
}
public static int maxDepthUsingDfs(TreeNode root) {
return dfs(root, 0);
}
private static int dfs(TreeNode node, int prevDepth) {
if(node==null) return prevDepth;
int leftDepth = dfs(node.left, prevDepth+1);
int rightDepth = dfs(node.right, prevDepth+1);
return Math.max(leftDepth, rightDepth); // max total depth
}
public static int maxDepthUsingRecursion(TreeNode root) {
if(root == null) return 0;
int left = maxDepthUsingRecursion(root.left);
int right = maxDepthUsingRecursion(root.right);
return Math.max(left, right) + 1; // +1 for current level
}
public static int maxDepthUsingRecursion2(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepthUsingRecursion2(root.left), maxDepthUsingRecursion2(root.right)) +1;
}
public static int maxDepthUsingDfs2(TreeNode root) {
if(root == null) return 0;
return dfs2(root, 1); // dfs2(root, 1) && return depth-1; in dfs2() or dfs2(root, 0) and return depth; in dfs2()
}
private static int dfs2(TreeNode node, int depth) {
if(node==null) return depth-1;
int leftDepth = dfs2(node.left, depth+1);
int rightDepth = dfs2(node.right, depth+1);
depth = Math.max(depth, leftDepth);
depth = Math.max(depth, rightDepth);
return depth;
}
public static int maxDepthUsingBfsQueue(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int depth = 0;
while(!q.isEmpty()) {
int size = q.size();
for(int i=0; i<size; i++) { // constant "i<size" means one level
TreeNode node = q.poll();
if(node.left != null) q.add(node.left);
if(node.right != null) q.add(node.right);
}
depth++;
}
// q.offer(null);
return depth;
}
public static int maxDepthUsingBfsQueue2(TreeNode root) {
class Pair {
final TreeNode node;
final int depth;
Pair(TreeNode node, int depth) {
this.node = node;
this.depth = depth;
}
}
if(root == null) return 0;
Queue<Pair> q = new LinkedList<>();
q.offer(new Pair(root, 1));
int depth = 0;
while(!q.isEmpty()) {
Pair pair = q.poll();
TreeNode node = pair.node;
depth = pair.depth;
if(node.left != null) q.add(new Pair(node.left, depth+1));
if(node.right != null) q.add(new Pair(node.right, depth+1));
}
return depth;
}
public static int maxDepthUsingStack(TreeNode root) {
class Pair {
final TreeNode node;
final int depth;
Pair(TreeNode node, int depth) {
this.node = node;
this.depth = depth;
}
}
if (root == null) return 0;
Stack<Pair> stack = new Stack<>();
stack.push(new Pair(root, 1));
int depth = 0;
while (!stack.isEmpty()) {
Pair pair = stack.pop();
TreeNode node = pair.node;
depth = Math.max(depth, pair.depth);
if (node.left != null) stack.push(new Pair(node.left, pair.depth + 1));
if (node.right != null) stack.push(new Pair(node.right, pair.depth + 1));
}
return depth;
}
}