leetcode——Lowest Common Ancestor of a Binary Tree

题目

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

思路

这一次说的是一个普通的二叉树,给出两个节点,求他们的最低公共父节点。

回想一下,当这棵二叉树是二分查找树的时候的解决方案:

二分查找树解法:http://blog.csdn.net/langduhualangdu/article/details/47426339

没错,不管是二分查找树也好还是普通二叉树也好,他们的共同规律就是:所给出的两个节点一定在最低公共父节点的两侧

那对于BST来说,可以通过大小进行比较判断是不是在当前节点的两侧,普通二叉树如何比较呢?

其实,我们可以从反面去考虑这个事情:如果当前节点的某一侧子树没有所给节点中的任何一个,那是不是就能肯定,该节点一定不是最低父节点(节点重合的情况另说),而且,所求节点一定在另一侧。

因此,我们可以这样:当前节点如果为null,返回null;如果为所给节点其中之一,则返回该节点;否则,求出当前节点左子树的返回值和右子数的返回值,如果左右值都不为空,说明当前节点即为所求节点,否则,返回不为空的那个节点。

同样,当得到所求节点后,还是需要检查所在的树上是不是同时存在所给的两个节点。应该比较的是节点的地址而不是值。

代码

public boolean checkExist(TreeNode root, TreeNode p, TreeNode q){
        if(root==null)
            return false;
        boolean pExist = false, qExist = false;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode treeNode = queue.poll();
            if(treeNode==p)
                pExist = true;
            if(treeNode==q)
                qExist = true;
            if(pExist && qExist)
                return true;
            if(treeNode.left!=null)
                queue.add(treeNode.left);
            if(treeNode.right!=null)
                queue.add(treeNode.right);

        }
        return false;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode candidateNode = search(root, p, q);
        if(checkExist(candidateNode,p,q))
            return candidateNode;
        else {
            return null;
        }
    }   

    public TreeNode search(TreeNode root, TreeNode p, TreeNode q){
        if(root==null)
            return null;
        if(root==p || root==q){
            return root;
        } else{
            TreeNode left = search(root.left, p, q);
            TreeNode right = search(root.right, p, q);
            if(left!=null && right!=null)
                return root;
            else {
                return left!=null?left:right;
            }
        }
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 13:31:41

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

[LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 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

Leetcode ——Lowest Common Ancestor of a Binary Tree

Question Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has bot

[LeetCode]Lowest Common Ancestor of a Binary Tree

第一个是普通二叉树,第二个是bst public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) { return null; } if (root == p || root == q) { return root; } TreeNode left = lowestCommonAncestor(root.left, p,

88 Lowest Common Ancestor of a Binary Tree

原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树,找到两个节点的最近公共父节点(LCA). 最近公共祖先是两个节点的公共的祖先节点且具有最大深度. 假设给出的两个节点都在树中存在 您在真实的面试中是否遇到过这个题?  是 样例 对于下面这棵二叉树 4 / 3 7 / 5 6 LCA(3, 5) = 4 LCA(5, 6) = 7 LCA(6, 7

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 Tree

Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as th

【LeetCode】236. Lowest Common Ancestor of a Binary Tree

Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as th

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*

Lowest Common Ancestor of a Binary Tree题解

这是LeetCode上的一道题,让我们先来看一看题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest nod