Leetcode 235.二叉搜索树的公共祖先

二叉搜索树的公共祖先

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。"

例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5]

示例 1:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8

输出: 6

解释: 节点 2 和节点 8 的最近公共祖先是 6。

示例 2:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4

输出: 2

解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。

说明:

  • 所有节点的值都是唯一的。
  • p、q 为不同节点且均存在于给定的二叉搜索树中。

如果如果p,q 比root小, 则LCA必定在左子树, 如果p,q比root大, 则LCA必定在右子树. 如果一大一小, 则root即为LCA.

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution{
11     public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
12         if(root==null||p==null||q==null) return null;
13         if(Math.max(p.val,q.val)<root.val){
14             return lowestCommonAncestor(root.left,p,q);
15         }else if(Math.min(p.val,q.val)>root.val){
16             return lowestCommonAncestor(root.right,p,q);
17         }else return root;
18     }
19 }

原文地址:https://www.cnblogs.com/kexinxin/p/10203096.html

时间: 2024-08-29 07:58:19

Leetcode 235.二叉搜索树的公共祖先的相关文章

leetcode 235 二叉搜索树最近公共祖先

leetcode 235 二叉搜索树最近公共祖先 题目描述: 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先.百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先). 解法一:自己的写法,贼傻 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self

LeetCode 235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree) 32

235. 二叉搜索树的最近公共祖先 235. Lowest Common Ancestor of a Binary Search Tree 题目描述 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)." 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,nul

leetcode 235. 二叉搜索树的最近公共祖先(c++)

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先).” 例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5] 示例 1: 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8输出: 6 解释: 节点

LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意: root 是树结点对象 (TreeNode object),而不是数组. 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / 2 6 / \ 1 3 最小的差

LeetCode 把二叉搜索树转换为累加树(538)

第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉搜索树: 5 / 2 13 输出: 转换为累加树: 18 / 20 13 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree 概念 二叉搜索树 二叉查找树(Binary Search Tree

Leetcode 验证二叉搜索树

给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索树. 示例 1: 输入: 2 / 1 3 输出: true 示例 2: 输入: 5 / 1 4 / 3 6 输出: false 解释: 输入为: [5,1,4,null,null,3,6].   根节点的值为 5 ,但是其右子节点值为 4 . 这道题我想的解答方案是错误的,后来参考了别人的 # De

235. 二叉搜索树的最近公共祖先

题目链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/submissions/ 解题思路: 如果p和q都小于root,去左边找就行. 如果p和q在两侧的,直接就是root,这个可以通过val来判断. 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * Tr

【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

解法一:递归 1 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 2 { 3 if (root == NULL || p == NULL || q == NULL) 4 return NULL; 5 6 if (root->val > p->val && root->val > q->val) 7 return lowestCommonAncestor(ro

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

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