fork download
  1. /**
  2.  * Lowest Common Ancestor of an N-ary Tree
  3.  *
  4.  * Approach: Bottom-up recursion / Postorder DFS
  5.  *
  6.  * Similar to LCA in a binary tree, but instead of searching
  7.  * only left and right subtrees, we search all child subtrees.
  8.  *
  9.  * Each recursive call returns:
  10.  * 1. null - if neither p nor q exists in the subtree.
  11.  * 2. p or q - if only one target exists in the subtree.
  12.  * 3. LCA - if both targets exist in the subtree.
  13.  *
  14.  * Assumption: Both target nodes exist in the original tree.
  15.  */
  16. var lowestCommonAncestor = function(root, p, q) {
  17. // Base case 1:
  18. // An empty subtree contains neither target.
  19. if (!root) return null;
  20.  
  21. // Base case 2:
  22. // If the current node is one of the targets, return it.
  23. // It may itself be the LCA if the other target is
  24. // one of its descendants.
  25. if (root === p || root === q) return root;
  26.  
  27. // Store non-null results returned by the child subtrees.
  28. // A result can be p, q, or an already determined LCA.
  29. const keyNodes = [];
  30.  
  31. // Unlike a binary tree, an N-ary tree can have any
  32. // number of children. Recursively search each child.
  33. for (const child of root.children) {
  34. const keyNode = lowestCommonAncestor(child, p, q);
  35.  
  36. // Ignore subtrees that contain neither target.
  37. if (keyNode !== null) {
  38. keyNodes.push(keyNode);
  39. }
  40. }
  41.  
  42. // Case 1: Two different child subtrees contain the targets.
  43. // The current node is the split point and therefore the LCA.
  44. if (keyNodes.length === 2) return root;
  45.  
  46. // Case 2: Only one child subtree returned a non-null result.
  47. // It may contain one target or an already determined LCA.
  48. // Simply propagate that result upward.
  49. if (keyNodes.length === 1) return keyNodes[0];
  50.  
  51. // Case 3: Neither the current node nor any of its
  52. // child subtrees contains either target.
  53. return null;
  54. };
  55. // 7. Time and space complexity
  56. // Time Complexity: O(N)
  57. // In the worst case, we visit every node in the tree once.
  58.  
  59. // Although there is a for loop inside the recursive function, the total number of child relationships in a tree with N nodes is N - 1.
  60.  
  61. // Therefore, the total work across all recursive calls is linear.
  62. // Space Complexity: O(H)
  63. // Here, H is the height of the tree.
  64.  
  65. // The recursion stack contains at most H active recursive calls (up to an additive constant, depending on the height convention).
  66.  
  67. // You might wonder whether creating a keyNodes array in every recursive call increases the space complexity.
  68.  
  69. // However, each keyNodes array can hold at most two nodes, because we are searching for only two targets. Therefore, each recursive call uses O(1) additional space.
  70. // \[
  71. // SC = O(H)
  72. // \]
  73.  
  74. // Worst-case space complexity is O(N) for a skewed tree.
Success #stdin #stdout 0.02s 5648KB
stdin
Standard input is empty
stdout
Standard output is empty