235. 二叉搜索树的最近公共祖先
/*** 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) {let x = root.valif(p.val<x&&q.val<x){return lowestCommonAncestor(root.left,p,q)}if(p.val>x&&q.val>x){return lowestCommonAncestor(root.right,p,q)}return root
};
执行用时分布
63ms
击败94.96%
使用 JavaScript 的用户消耗内存分布
59.35MB
击败86.26%
使用 JavaScript 的用户
参考链接
235. 二叉搜索树的最近公共祖先