Jan 12 - Lowest Common Ancestor of a Binary Search Tree; Tree; BST; Recursion;

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

  

时间: 2024-08-26 14:21:14

Jan 12 - Lowest Common Ancestor of a Binary Search Tree; Tree; BST; Recursion;的相关文章

[LeetCode][JavaScript]Lowest Common Ancestor of a Binary Search Tree

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

Lowest Common Ancestor of a Binary Search Tree &amp; a Binary Tree

235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description 题目大意:给定一棵二叉查找树和两个节点p和q,要求返回这两个节点的第一个公共祖先. 思路:对于当前节点node,如果p和q的val都大于node的val,则说明p和q的第一个公共祖先在node的右子树,如

[geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor in a Binary Search Tree. Given values of two nodes in a Binary Search Tree, write a c program to find the Lowest Common Ancestor (LCA). You may assume

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

leetcode - Lowest Common Ancestor of a Binary Search Tree

leetcode -  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

leetcode_235——Lowest Common Ancestor of a Binary Search Tree(二叉排序树)

Lowest Common Ancestor of a Binary Search Tree Total Accepted: 7402 Total Submissions: 19069My Submissions Question Solution Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the defin

【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

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

235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

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

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Search Tree

236. Lowest Common Ancestor of a Binary Tree 递归寻找p或q,如果找到,层层向上返回,知道 root 左边和右边都不为NULL:if (left!=NULL && right!=NULL) return root; 时间复杂度 O(n),空间复杂度 O(H) class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode*