|
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 | 12 | * time O(n) | space: O(h), where h is the height of the deepest tree |
13 | 13 | * |
14 | | - * Implementing idea #2 |
| 14 | + * Implementing idea #2... |
15 | 15 | * |
16 | 16 | * After reading the solution we can do better: time O(n) and space O(1) |
17 | 17 | * |
@@ -57,41 +57,44 @@ function getAncestor(node, node1, node2) { |
57 | 57 | let matchingNode = null; |
58 | 58 | let isAncestor = false; |
59 | 59 |
|
60 | | - if(node) { |
61 | | - const left = getAncestor(node.left, node1, node2); |
62 | | - if (left.isAncestor) { |
63 | | - return left; |
64 | | - } |
| 60 | + if(!node) { |
| 61 | + return {matchingNode, isAncestor}; |
| 62 | + } |
| 63 | + |
| 64 | + const left = getAncestor(node.left, node1, node2); |
| 65 | + if (left.isAncestor) { |
| 66 | + return left; |
| 67 | + } |
65 | 68 |
|
66 | | - const right = getAncestor(node.right, node1, node2); |
67 | | - if (right.isAncestor) { |
68 | | - return right; |
69 | | - } |
| 69 | + const right = getAncestor(node.right, node1, node2); |
| 70 | + if (right.isAncestor) { |
| 71 | + return right; |
| 72 | + } |
70 | 73 |
|
71 | | - if(node === node1 && node2 === node1) { |
| 74 | + if(node === node1 && node2 === node1) { |
72 | 75 |
|
73 | | - matchingNode = node; |
74 | | - isAncestor = true; |
| 76 | + // already found both nodes since they are the same |
| 77 | + matchingNode = node; |
| 78 | + isAncestor = true; |
75 | 79 |
|
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) { |
| 80 | + } else if(left.matchingNode && right.matchingNode || |
| 81 | + left.matchingNode === node1 && node === node2 || left.matchingNode === node2 && node === node1 || |
| 82 | + right.matchingNode === node1 && node === node2 || right.matchingNode === node2 && node === node1) { |
79 | 83 |
|
80 | | - // if we found both nodes already then we found an ancestor |
81 | | - matchingNode = node; |
82 | | - isAncestor = true; |
| 84 | + // if we found both nodes already then we found an ancestor |
| 85 | + matchingNode = node; |
| 86 | + isAncestor = true; |
83 | 87 |
|
84 | | - } else if(node === node1 || node === node2) { |
| 88 | + } else if(node === node1 || node === node2) { |
85 | 89 |
|
86 | | - // set match |
87 | | - matchingNode = node; |
| 90 | + // set match |
| 91 | + matchingNode = node; |
88 | 92 |
|
89 | | - } else { |
| 93 | + } else { |
90 | 94 |
|
91 | | - // bubble up partial match |
92 | | - return left.matchingNode ? left : right; |
| 95 | + // bubble up partial match |
| 96 | + return left.matchingNode ? left : right; |
93 | 97 |
|
94 | | - } |
95 | 98 | } |
96 | 99 |
|
97 | 100 | return {matchingNode, isAncestor}; |
|
0 commit comments