Skip to content

walunjakarsachinedu/code-problem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

1519. Number of Nodes in the Sub-Tree With the Same Label


Main idea to solve problem: Find recursive relationship with subproblem

Algorithm:

// label_nodes: map {label_name : [all ancestor node with label equal to label_name]}
// ans: stores number of nodes in sub-tree with same label

function countSubTrees(root):
  label_nodes[labels[root]].append(root); 
  for each node in label_node[labels[root]]: ++ans[node];
  for each neighbour of root: countSubTrees(neighbour);
  label_nodes[labels[root]].remove(root); 

  return ans;

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages