美文网首页
LeetCode关于节点公共祖先的问题

LeetCode关于节点公共祖先的问题

作者: 专职跑龙套 | 来源:发表于2018-04-03 14:46 被阅读27次

关于我的 Leetcode 题目解答,代码前往 Github:https://github.com/chenxiangcyr/leetcode-answers


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

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

LeetCode题目:236. 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 as descendants (where we allow a node to be a descendant of itself).”

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

相关文章

  • LeetCode关于节点公共祖先的问题

    关于我的 Leetcode 题目解答,代码前往 Github:https://github.com/chenxia...

  • <剑指Offer>面试题68: 树中两个节点的最低公共祖先

    题目描述 LeetCode 236 输入两个树节点,求它们的最低公共祖先 题目解读 剑指Offer 327 思路一...

  • 二叉树的最近公共祖先

    LeetCode 二叉树最近公共祖先 题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科...

  • 最近公共祖先系列

    最近公共祖先I 描述: 给定一棵二叉树,找到两个节点的最近公共父节点 (LCA)。最近公共祖先是两个节点的公共的祖...

  • LCA_最近公共祖先

    LCA 最近公共祖先在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上...

  • lintcode 最近公共祖先

    给定一棵二叉树,找到两个节点的最近公共父节点(LCA)。最近公共祖先是两个节点的公共的祖先节点且具有最大深度。样例...

  • LCA(最近公共祖先)算法

    在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先...

  • 最近公共祖先(LCA-tarjan/RMQ/倍增)

    在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先...

  • 二叉树-最近的公共祖先

    已知二叉树,求二叉树中给定的两个节点的最近公共祖先。 最近公共祖先: 两节点v与w的最近公共祖先u,满足在树上最低...

  • 公共祖先问题

    前言:逃不开的「公共祖先」问题 0X00 一次查询 236. 二叉树的最近公共祖先 用 set 写比较简单,注意其...

网友评论

      本文标题:LeetCode关于节点公共祖先的问题

      本文链接:https://www.haomeiwen.com/subject/bnaqhftx.html