235.二叉搜索树的最近公共祖先
var lowestCommonAncestor = function (root, p, q) {
function getGrands(root, n) {
let grands = []
let cur = root
while (cur) {
grands.push(cur)
if (n.val === cur.val) {
return grands
} else if (n.val < cur.val) {
cur = cur.left
} else if (n.val > cur.val) {
cur = cur.right
}
}
return []
}
let g1 = getGrands(root, p)
let g2 = getGrands(root, q)
console.log(g1.map(e => e.val), g2.map(e => e.val));
while (g1.length) {
let node = g1.pop()
for (let i = g2.length - 1; i >= 0; i--) {
if (node.val === g2[i].val) {
return node
}
}
}
};