Company: Google
Stage: Phone Screen
Given a tree having nodes with value 0 and 1. write a function to return the number of islands ?
Follow Up Questions: (Asked by me)
My Approach:
Defined the custom node as follows:
class TreeNode{
int value;
List<TreeNode> nextNodes;
// Constructor
TreeNode(int value){
this.value = value;
this.nextNodes = new LinkedList<>();
}
}
Given DFS Approach by maintaining the Visited Set of nodes.
Interviewer expected the O(n) Time complexity and O(1) Space Complexity
I am unable to find the question in leetcode. Can anyone please suggest me better appraoch here ?