From c5b691b91dc85cc16304fb9884d9db4afadb88e0 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Wed, 8 Dec 2021 16:49:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 0236题 Java版本代码 --- .../0236.二叉树的最近公共祖先.md | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 4182999f..6d26e376 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -221,42 +221,30 @@ public: ## Java + ```Java class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - return lowestCommonAncestor1(root, p, q); - } - public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) { - if (root == null || root == p || root == q) { + if (root == null || root == p || root == q) { // 递归结束条件 return root; } - TreeNode left = lowestCommonAncestor1(root.left, p, q); - TreeNode right = lowestCommonAncestor1(root.right, p, q); - if (left != null && right != null) {// 左右子树分别找到了,说明此时的root就是要求的结果 - return root; - } - if (left == null) { + + // 后序遍历 + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + + if(left == null && right == null) { // 若未找到节点 p 或 q + return null; + }else if(left == null && right != null) { // 若找到一个节点 return right; + }else if(left != null && right == null) { // 若找到一个节点 + return left; + }else { // 若找到两个节点 + return root; } - return left; } } -``` - -```java -// 代码精简版 -class Solution { - public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - if (root == null || root.val == p.val ||root.val == q.val) return root; - TreeNode left = lowestCommonAncestor(root.left,p,q); - TreeNode right = lowestCommonAncestor(root.right,p,q); - if (left != null && right != null) return root; - else if (left == null && right != null) return right; - else if (left != null && right == null) return left; - else return null; - } -} ``` ## Python From 38b8b343c931019e76d7692b99ee7d397538494c Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Thu, 9 Dec 2021 11:16:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=A2=98=E7=9B=AE0701?= =?UTF-8?q?=20Java=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0701.二叉搜索树中的插入操作.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 5d63ce58..1e6ab47e 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -236,16 +236,13 @@ class Solution { ```java class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { - return buildTree(root, val); - } - - public TreeNode buildTree(TreeNode root, int val){ if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。 return new TreeNode(val); + if (root.val < val){ - root.right = buildTree(root.right, val); // 递归创建右子树 + root.right = insertIntoBST(root.right, val); // 递归创建右子树 }else if (root.val > val){ - root.left = buildTree(root.left, val); // 递归创建左子树 + root.left = insertIntoBST(root.left, val); // 递归创建左子树 } return root; }