mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #936 from zhicheng-lee/master
Update 0236.二叉树的最近公共祖先.md
This commit is contained in:
@ -221,42 +221,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
## Java
|
## Java
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
return lowestCommonAncestor1(root, p, q);
|
if (root == null || root == p || root == q) { // 递归结束条件
|
||||||
}
|
|
||||||
public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
|
|
||||||
if (root == null || root == p || root == q) {
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
TreeNode left = lowestCommonAncestor1(root.left, p, q);
|
|
||||||
TreeNode right = lowestCommonAncestor1(root.right, p, q);
|
// 后序遍历
|
||||||
if (left != null && right != null) {// 左右子树分别找到了,说明此时的root就是要求的结果
|
TreeNode left = lowestCommonAncestor(root.left, p, q);
|
||||||
return root;
|
TreeNode right = lowestCommonAncestor(root.right, p, q);
|
||||||
}
|
|
||||||
if (left == null) {
|
if(left == null && right == null) { // 若未找到节点 p 或 q
|
||||||
|
return null;
|
||||||
|
}else if(left == null && right != null) { // 若找到一个节点
|
||||||
return right;
|
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
|
## Python
|
||||||
|
@ -236,16 +236,13 @@ class Solution {
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public TreeNode insertIntoBST(TreeNode root, int val) {
|
public TreeNode insertIntoBST(TreeNode root, int val) {
|
||||||
return buildTree(root, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TreeNode buildTree(TreeNode root, int val){
|
|
||||||
if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
|
if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
|
||||||
return new TreeNode(val);
|
return new TreeNode(val);
|
||||||
|
|
||||||
if (root.val < val){
|
if (root.val < val){
|
||||||
root.right = buildTree(root.right, val); // 递归创建右子树
|
root.right = insertIntoBST(root.right, val); // 递归创建右子树
|
||||||
}else if (root.val > val){
|
}else if (root.val > val){
|
||||||
root.left = buildTree(root.left, val); // 递归创建左子树
|
root.left = insertIntoBST(root.left, val); // 递归创建左子树
|
||||||
}
|
}
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user