Count Special Nodes in Generic Tree

Given a generic tree, find the count of all special nodes. A node is a special node if there is a path from root to that node with all distinct elements. The input was not a pointer to a tree. He’d give me an adjacency list and an array of values where the value of ith node in the adjacency list is the ith element in the values array. He asked me not a create a tree out of the given information and rather do it with the adjacency list itself. 

CodingNinjas
author
2y

I suggested to do a depth first search keeping a set which contains all elements upto a given node. Once I reach a particular node, I check if it’s already in the set. If its already in the set, I ret...read more

CodingNinjas
author
2y
DFS

We will do a recursive Depth First Search keeping a vector/list to check for the distinct element.

  1. We start the recursive DFS from the “root” node with an empty vector/list.
  2. Let’s say we are at some ...read more
CodingNinjas
author
2y
HashMap/Dictionary Approach

We will do a recursive Depth First Search keeping a HashMap/Dictionary to check for the distinct element.

  1. We start the recursive DFS from the “root” node with an empty HashMa...read more
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter