Lowest Common Ancestor III

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.

java

/**
 * 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.
     */
    public TreeNode lowestCommonAncestor3(TreeNode root, TreeNode A, TreeNode B) {
        // write your code here
        if (root == null || A == null || B == null) {
            return null;
        }
        boolean a = findNode(root, A);
        boolean b = findNode(root, B);
        if (!a || !b) {
            return null;
        }
        return findRoot(root, A, B);
    }
    
    private TreeNode findRoot(TreeNode root, TreeNode A, TreeNode B) {
        if (root == null) {
            return null;
        }
        if (root == A || root == B) {
            return root;
        }
        TreeNode left = findRoot(root.left, A, B);
        TreeNode right = findRoot(root.right, A, B);
        if (left != null && right != null) {
            return root;
        }
        if (left != null) {
            return left;
        }
        if (right != null) {
            return right;
        }
        return null;
    }
    
    private boolean findNode(TreeNode root, TreeNode node) {
        if (root == null) {
            return false;
        }
        if (root == node) {
            return true;
        }
        boolean left = findNode(root.left, node);
        boolean right = findNode(root.right, node);
        return left || right;
    }
}

python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        if root is None or p is None or q is None:
            return None
        if not self.findNode(root, p) or not self.findNode(root, q):
            return None
        if root == q or root == p:
            return root
        return self.findRoot(root, p, q)
    
    def findRoot(self, root, p, q):
        if root is None:
            return None
        if root == p or root == q:
            return root
        left = self.findRoot(root.left, p, q)
        right = self.findRoot(root.right, p, q)
        if left is not None and right is not None:
            return root
        if left is not None:
            return left
        if right is not None:
            return right
        return None
    
    def findNode(self, root, node):
        if root is None:
            return False
        if root == node:
            return True
        left = self.findNode(root.left, node)
        right = self.findNode(root.right, node)
        return (left or right)
        


你可能感兴趣的:(leetcode)