Problem
104. Maximum Depth of Binary Tree
Java
Depth-first Search - Recursion
1 | /** |
Submission Detail
- 39 / 39 test cases passed.
- Runtime: 0 ms, faster than 100.00% of Java online submissions for Maximum Depth of Binary Tree.
- Memory Usage: 39 MB, less than 49.45% of Java online submissions for Maximum Depth of Binary Tree.
DFS
© Finding max depth of binary tree without recursion
This variant uses two stacks, one for additional nodes to explore (wq) and one always containing the current path from the root (path). When we see the same node on the top of both stacks it means we’ve explored everything below it and can pop it. This is the time to update the tree depth too.
1 | /** |
Submission Detail
- 39 / 39 test cases passed.
- Runtime: 3 ms, faster than 5.14% of Java online submissions for Maximum Depth of Binary Tree.
- Memory Usage: 38.6 MB, less than 96.88% of Java online submissions for Maximum Depth of Binary Tree.
BFS
© Day 39— Max Depth of Binary Tree Using BFS
1 | /** |
Submission Detail
- 39 / 39 test cases passed.
- Runtime: 1 ms, faster than 14.50% of Java online submissions for Maximum Depth of Binary Tree.
- Memory Usage: 38.6 MB, less than 92.61% of Java online submissions for Maximum Depth of Binary Tree.