-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
67 lines (62 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
if (root === p || root === q) {
return root;
}
let p_path = [];
found_path(root,p_path,p);
return p_path.find((node) => {
return in_parent_path(node, q);
});
function in_parent_path(root, node) {
if (root === node) {
return true;
}
let result = root.left && in_parent_path(root.left, node);
if (result) {
return true;
}
return root.right && in_parent_path(root.right, node);
}
function found_path(root, paths, target) {
if (root === target) {
paths.push(root);
return true;
}
let result = root.left && found_path(root.left, paths, target);
if (result) {
paths.push(root);
return result;
}
result = root.right && found_path(root.right, paths, target);
if (result) {
paths.push(root);
}
return result;
}
};
const arr = [3,5,1,6,2,0,8,null,null,7,4];
const { make_tree } = require('../utils');
const test = make_tree(arr, [0,1,8]);
const root = test[0];
const p = test[1];
const q = test[2];
const result = lowestCommonAncestor(root, p, q);
module.exports = {
id:'236',
title:'Lowest Common Ancestor of a Binary Tree',
url:'https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/',
difficulty:'medium',
};