From 53ce92f95f999904127340f8a315feb33a43d03f Mon Sep 17 00:00:00 2001 From: "xeniaxie(xl)" Date: Sun, 27 Jun 2021 10:45:48 +0800 Subject: [PATCH] =?UTF-8?q?0235=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88Javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.二叉搜索树的最近公共祖先.md | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index d78db42a..8ea3cdc5 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -312,8 +312,46 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { }else {return findLeft} } ``` - - +JavaScript版本: +1. 使用递归的方法 +```javascript +var lowestCommonAncestor = function(root, p, q) { + // 使用递归的方法 + // 1. 使用给定的递归函数lowestCommonAncestor + // 2. 确定递归终止条件 + if(root === null) { + return root; + } + if(root.val>p.val&&root.val>q.val) { + // 向左子树查询 + let left = lowestCommonAncestor(root.left,p,q); + return left !== null&&left; + } + if(root.valp.val&&root.val>q.val) { + root = root.left; + }else if(root.val