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 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 both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______6______
       /                  ___2__          ___8__
   /      \        /         0      _4       7       9
         /           3   5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

4 My code(AC)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {

    public boolean DFS(TreeNode root, TreeNode target, LinkedList<TreeNode> path)
    {

        if (root == null || target == null)
            return false;

        path.addLast(root);
        if (root == target) {
            return true;
        }

        if (DFS(root.left, target, path)) {
            return true;
        } else {
            if (root.left != null) {
                path.removeLast();
            }
            boolean re =  DFS(root.right, target, path);
            if( root.right != null && re == false){
                path.removeLast();
            }
            return re;
        }

    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {

        if( p == null || q == null)
        {
            return null;
        }
        LinkedList<TreeNode> path1 = new LinkedList<TreeNode>();
        LinkedList<TreeNode> path2 = new LinkedList<TreeNode>();
        DFS(root,p,path1);
        DFS(root,q,path2);

        if( path1.size() <= 0 || path2.size() <= 0)
            return null;
        TreeNode bf = path1.removeFirst();
        path2.removeFirst();
        while( !path1.isEmpty() && !path2.isEmpty()){
            TreeNode node1 = path1.removeFirst();
            TreeNode node2 = path2.removeFirst();
            if( node1 != node2){
                break;
            }
            bf =  node1;
        }
        return bf;

    }
}
时间: 2024-08-02 19:12:01

Lowest Common Ancestor of a Binary Search Tree的相关文章

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][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的右子树,如

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

[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

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*

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