[LeetCode][JavaScript]Lowest Common Ancestor of a Binary Search Tree
Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5
For example, the lowest common ancestor (LCA) of nodes 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, since a node can be a descendant of itself according to the LCA definition.
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
传说中的经典问题...经典解法:
2轮遍历,记录下p和q的路径,比较路径输出不同的那个节点。
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @param {TreeNode} p 11 * @param {TreeNode} q 12 * @return {TreeNode} 13 */ 14 var lowestCommonAncestor = function(root, p, q) { 15 var pathP = []; 16 var pathQ = []; 17 findLCA(root, p.val, pathP); 18 findLCA(root, q.val, pathQ); 19 var len = Math.min(pathP.length, pathQ.length); 20 for(var i = 0; i <= len; i++){ 21 if(!pathP[i] || !pathQ[i] || pathP[i].val !== pathQ[i].val){ 22 return pathP[--i]; 23 } 24 } 25 26 function findLCA(node, target, list){ 27 list.push(node); 28 if(target < node.val){ 29 findLCA(node.left, target, list); 30 }else if(target > node.val){ 31 findLCA(node.right, target, list); 32 } 33 } 34 };
第一遍写了这么个破东西。
先看左子树里有没有其中一个,如果有先记下,然后看右子树里有没有,加上左子树的结果判断是否找到,
最后加上目前节点的val,看是否找到。
虽然一轮遍历就可以知道结果,但是超级复杂,写了好长,没有用到BST的性质...
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @param {TreeNode} p 11 * @param {TreeNode} q 12 * @return {TreeNode} 13 */ 14 var lowestCommonAncestor = function(root, p, q) { 15 var target = [], res, isFound = false; 16 target.push(p.val); 17 target.push(q.val); 18 findLCA(root); 19 return res; 20 21 function findLCA(node){ 22 if(isFound){ 23 return []; 24 } 25 26 var candidates = []; 27 if(node.left !== null){ 28 var left = findLCA(node.left); 29 left = compareVal(left); 30 if(left === true){ 31 res = node.left; 32 isFound = true; 33 return []; 34 }else if(left.length === 1){ 35 candidates= left; 36 } 37 } 38 39 if(node.right !== null && !isFound){ 40 var right = findLCA(node.right); 41 if(right && right.length === 1){ 42 candidates.push(right[0]); 43 } 44 right = compareVal(candidates); 45 if(right === true){ 46 res = node; 47 isFound = true; 48 return []; 49 }else if(right.length === 1){ 50 candidates = right; 51 } 52 } 53 54 if(!isFound){ 55 candidates.push(node.val); 56 var curr = compareVal(candidates); 57 if(curr === true){ 58 res = node; 59 isFound = true; 60 return []; 61 } 62 return curr; 63 } 64 65 } 66 function compareVal(a, b){ 67 if(typeof a === "object"){ 68 if(a.length === 2){ 69 b = a[1]; 70 a = a[0]; 71 }else if(a.length === 1){ 72 a = a[0]; 73 } 74 } 75 var index1 = target.indexOf(a); 76 var index2 = target.indexOf(b); 77 if(index1 !== -1 && index2 !== -1 && (index1 !== index2 || a === b)){ 78 return true; 79 }else{ 80 if(index1 !== -1){ 81 return [a]; 82 }else if(index2 !== -1){ 83 return [b]; 84 } 85 } 86 return []; 87 } 88 };