Leetcode 235

思路1:对于一棵二叉排序树

1.如果当前节点的值小于p,q的值,那么LCA一定在root的右边;

2.如果当前节点的值大于p,q的值,那么LCA一定在root的左边;

3.如果当前节点的值在p,q的值之间,那么当前节点为LCA;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root->val<p->val&&root->val<q->val)
            return lowestCommonAncestor(root->right,p,q);
        else if(root->val>p->val&&root->val>q->val)
            return lowestCommonAncestor(root->left,p,q);
        else return root;
    }
};

思路2:直接搜索两个数p,q的值,记录下搜索过程的路径,左侧为-1,右侧为1.比对路径即可,路径开始不同的点即为LCA(即交叉点)

思路3:对于一棵二叉排序树,如果p,q均在当前节点左字树或者右子树,则根节点与p,q的值的差值同号,乘积大于零。

然后继续判断在左边还是在右边

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    while((root->val-p->val)*(root->val-q->val)>0){
        if(root->val-p->val>0)
            root=root->left;
        else
            root=root->right;
    }
    return root;
}
时间: 2024-08-07 01:40:51

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 BST的最近公共祖先

这个题目比一般二叉树的最近公共祖先更佳的具体化,因为左右遍历路径都可以提前确定. 由于BST TREE的特点 1.当p,q节点在root的不同子树上的时候,root就位根节点 2.p或者q就是root的时候,返回root 3.p,q同时在root的左子树或者右子树,那么就递归遍历左子树或者右子树 1 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 2 while( (root->val - p-

Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树

给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / ___2__ ___8__ / \ / 0 4 7 9 / 3 5 2和8的最近公共祖先是6,2和4的最近公共祖先是2,假设找的3和5 TreeNode* l =lowestCommonAncestor(root->left,p,q) ; TreeNode* r =lowestCommonAncestor(root->right,p,q) ; 返回到4时两个都不是Nullptr,那么要返回4的指针 即

Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

递归实现如下: public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val>root.val&&q.val>root.val) return lowestCommonAncestor(root.right,p,q); else if(p.val<root.val&&q.val<root.val) return lowestCommonAn

(easy)LeetCode 235.Lowest Common Ancestor of a Binary Search Tree

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

Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree

题目描述: 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 tha

【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree

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

3.2 Lowest Common Ancestor of a Binary Search Tree (LeetCode 235)

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