Lowest Common Ancestor III

Note:

This is question is very similar to LCA original. The only difference is that the node may not exist. So if the node is not exsit, of course the result will be null. So in order to check the exisitance, we need to use resultType. In the previous question, when we found null/A/B, we can return root. But here, we first need to process the null. Then we need to do the divide. After that, we will verify whether that node is A/B, because we need to determine the existance of A/B.

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root The root of the binary tree.
     * @param A and B two nodes
     * @return: Return the LCA of the two nodes.
     */
    private class ResultType {
        TreeNode node;
        boolean isAExist;
        boolean isBExist;
        public ResultType(TreeNode node, boolean isAExist, boolean isBExist) {
            this.node = node;
            this.isAExist = isAExist;
            this.isBExist = isBExist;
        }
    }
    public TreeNode lowestCommonAncestor3(TreeNode root, TreeNode A, TreeNode B) {
        // write your code here
        ResultType node = findLCA(root, A, B);
        if (node.isAExist && node.isBExist) {
            return node.node;
        }
        return null;
    }
    private ResultType findLCA(TreeNode root, TreeNode A, TreeNode B) {
        if (root == null) {
            return new ResultType(null, false, false);
        }

        ResultType left = findLCA(root.left, A, B);
        ResultType right = findLCA(root.right, A, B);

        boolean isAExist = left.isAExist || right.isAExist || root == A;
        boolean isBExist = left.isBExist || right.isBExist || root == B;

        if (root == A || root == B) {
            return new ResultType(root, isAExist, isBExist);
        }

        if (left.node != null && right.node != null) {
            return new ResultType(root, isAExist, isBExist);
        }

        if (left.node != null) {
            return new ResultType(left.node, isAExist, isBExist);
        }

        if (right.node != null) {
            return new ResultType(right.node, isAExist, isBExist);
        }

        return new ResultType(null, isAExist, isBExist);
    }
}
时间: 2024-12-30 11:47:26

Lowest Common Ancestor III的相关文章

Lowest Common Ancestor III Lintcode

Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.Return null if LCA does not exist. Notice node A or nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 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 th

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

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree

题目如下:https://leetcode.com/problems/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 def

LeetCode (236):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

LeetCode OJ: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 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

4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest