Java Python Trees
Category: Trees & Graphs
This guide covers five popular interview questions in both Java and Python.
Max Depth
Overview
Max Depth is one of the most frequently asked coding interview questions.
Algorithm
DFS.
Complexity: O(n)
Tip: Recursive traversal.
Python
class Solution:
def solve(self,*args):
# TODO: implement Max Depth
pass
Java
class Solution {
// TODO: implement Max Depth
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Invert Tree
Overview
Invert Tree is one of the most frequently asked coding interview questions.
Algorithm
Swap children.
Complexity: O(n)
Tip: Simple recursion.
Python
class Solution:
def solve(self,*args):
# TODO: implement Invert Tree
pass
Java
class Solution {
// TODO: implement Invert Tree
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Level Order
Overview
Level Order is one of the most frequently asked coding interview questions.
Algorithm
BFS.
Complexity: O(n)
Tip: Queue.
Python
class Solution:
def solve(self,*args):
# TODO: implement Level Order
pass
Java
class Solution {
// TODO: implement Level Order
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Number of Islands
Overview
Number of Islands is one of the most frequently asked coding interview questions.
Algorithm
Flood fill.
Complexity: O(mn)
Tip: DFS each island.
Python
class Solution:
def solve(self,*args):
# TODO: implement Number of Islands
pass
Java
class Solution {
// TODO: implement Number of Islands
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Clone Graph
Overview
Clone Graph is one of the most frequently asked coding interview questions.
Algorithm
Visited map.
Complexity: O(V+E)
Tip: Prevent cycles.
Python
class Solution:
def solve(self,*args):
# TODO: implement Clone Graph
pass
Java
class Solution {
// TODO: implement Clone Graph
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.