|
9 | 9 | * time: O(n^2) | space: O(1) |
10 | 10 | * |
11 | 11 | * idea # 2 use dfs to generate the ancestors of the two nodes in question. Then compare whats their first common ancestor if any. |
12 | | - * time O(n) | space: O(log(n)) height |
| 12 | + * time O(n) | space: O(h), where h is the height of the deepest tree |
13 | 13 | * |
14 | 14 | * Implementing idea #2 |
15 | 15 | * |
| 16 | + * After reading the solution we can do better: time O(n) and space O(1) |
| 17 | + * |
| 18 | + * 1. Use DFS to iterate through the tree |
| 19 | + * 2. if a current node matches either node1 or node2, then return it |
| 20 | + * 3. if doesn't match return null |
| 21 | + * 4. if a a side of the tree has previously return data then keep the info it has a ancestor |
| 22 | + * |
| 23 | + * Test cases |
| 24 | + * 1. Both nodes are on the tree |
| 25 | + * 2. One or both nodes are not in the tree |
| 26 | + * 3. One node is the ancestor of the other |
| 27 | + * |
| 28 | + * |
16 | 29 | * @param graph |
17 | 30 | * @param node1 |
18 | 31 | * @param node2 |
19 | 32 | */ |
20 | 33 | function getFirstCommonAncestor(graph, dataOrNode1, dataOrNode2) { |
21 | | - let ancestors1 = []; |
22 | | - let ancestors2 = []; |
23 | 34 | const node1 = graph.getNode(dataOrNode1); |
24 | 35 | const node2 = graph.getNode(dataOrNode2); |
| 36 | + let result = null; |
25 | 37 |
|
26 | | - graph.getNodes().some(function (node) { |
27 | | - ancestors1 = []; |
28 | | - ancestors2 = []; |
29 | | - getAncestors(node, node1, node2, ancestors1, ancestors2); |
30 | | - // console.log(node.data, ancestors1.map((d)=>d.data), ancestors2.map((d)=>d.data)) |
31 | | - return ancestors1.length && ancestors2.length; |
| 38 | + const isFound = graph.getNodes().some(function (node) { |
| 39 | + result = getAncestor(node, node1, node2); |
| 40 | + return result.isAncestor; |
32 | 41 | }); |
33 | 42 |
|
34 | | - return findLastCommon(ancestors1, ancestors2); |
35 | | -} |
36 | | - |
37 | | -function findLastCommon(array1, array2) { |
38 | | - const end = Math.min(array1.length, array2.length); |
39 | | - |
40 | | - for(let i = end; i > 0; i--) { |
41 | | - if(array1[i-1] === array2[i-1]) { |
42 | | - return array1[i-1]; |
43 | | - } |
| 43 | + if(isFound) { |
| 44 | + return result.matchingNode; |
44 | 45 | } |
| 46 | + |
45 | 47 | } |
46 | 48 |
|
47 | 49 | /** |
48 | 50 | * |
49 | 51 | * @param node |
50 | 52 | * @param node1 |
51 | 53 | * @param node2 |
52 | | - * @param ancestors1 |
53 | | - * @param ancestors2 |
54 | | - * @returns {number} 0 none, 1 node1, 2 node2, 3 node1 and node2 |
| 54 | + * @returns {matchingNode: Node, isAncestor: boolean} Node matches either Node1, Node2 or common ancestor (if flag is true). |
55 | 55 | */ |
56 | | -function getAncestors(node, node1, node2, ancestors1, ancestors2) { |
57 | | - if(node === node1) { |
58 | | - ancestors1.unshift(node); |
59 | | - return 1; |
60 | | - } else if (node === node2) { |
61 | | - ancestors2.unshift(node); |
62 | | - return 2; |
63 | | - } else { |
64 | | - let nodesFound = 0; |
65 | | - |
66 | | - node.adjacents.forEach(function (adj) { |
67 | | - const found = getAncestors(adj, node1, node2, ancestors1, ancestors2); |
68 | | - |
69 | | - if(found === 1 || nodesFound === 3) { |
70 | | - ancestors1.unshift(node); |
71 | | - |
72 | | - if(nodesFound === 2) { |
73 | | - nodesFound = 3; |
74 | | - } else { |
75 | | - nodesFound = 1; |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - if(found === 2 || nodesFound === 3) { |
80 | | - ancestors2.unshift(node); |
81 | | - |
82 | | - if(nodesFound === 1) { |
83 | | - nodesFound = 3; |
84 | | - } else { |
85 | | - nodesFound = 2; |
86 | | - } |
87 | | - } |
88 | | - }); |
89 | | - |
90 | | - return nodesFound; |
| 56 | +function getAncestor(node, node1, node2) { |
| 57 | + let matchingNode = null; |
| 58 | + let isAncestor = false; |
| 59 | + |
| 60 | + if(node) { |
| 61 | + const left = getAncestor(node.left, node1, node2); |
| 62 | + if (left.isAncestor) { |
| 63 | + return left; |
| 64 | + } |
| 65 | + |
| 66 | + const right = getAncestor(node.right, node1, node2); |
| 67 | + if (right.isAncestor) { |
| 68 | + return right; |
| 69 | + } |
| 70 | + |
| 71 | + if(node === node1 && node2 === node1) { |
| 72 | + |
| 73 | + matchingNode = node; |
| 74 | + isAncestor = true; |
| 75 | + |
| 76 | + } else if(left.matchingNode && right.matchingNode || |
| 77 | + left.matchingNode === node1 && node === node2 || left.matchingNode === node2 && node === node1 || |
| 78 | + right.matchingNode === node1 && node === node2 || right.matchingNode === node2 && node === node1) { |
| 79 | + |
| 80 | + // if we found both nodes already then we found an ancestor |
| 81 | + matchingNode = node; |
| 82 | + isAncestor = true; |
| 83 | + |
| 84 | + } else if(node === node1 || node === node2) { |
| 85 | + |
| 86 | + // set match |
| 87 | + matchingNode = node; |
| 88 | + |
| 89 | + } else { |
| 90 | + |
| 91 | + // bubble up partial match |
| 92 | + return left.matchingNode ? left : right; |
| 93 | + |
| 94 | + } |
91 | 95 | } |
| 96 | + |
| 97 | + return {matchingNode, isAncestor}; |
92 | 98 | } |
93 | 99 |
|
94 | 100 |
|
|
0 commit comments