Google | Phone Screen | Number of Islands in a Tree
Anonymous User
7549

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)

  1. Are we giving root node as start node or it can vary ?
  2. Maximum number of children nodes for the node ?

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 ?

Comments (22)