LeetCode--236--medium--lowestCommonAncestor

Summary:

approach1: print the path of p and q | dfs | backtrack | O(N)

approach2:beautifu recursion | O(N)

package com.odyssey.app.algorithm.lc.tree;

import com.odyssey.app.algorithm.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dingsheng Huang
 * @date 2020/3/15 1:18
 *
 * 236
 * medium
 * 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 defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
 *
 * Given the following binary tree:  root = [3,5,1,6,2,0,8,null,null,7,4]
 *
 *
 *
 *
 * Example 1:
 *
 * Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
 * Output: 3
 * Explanation: The LCA of nodes 5 and 1 is 3.
 * Example 2:
 *
 * Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
 * Output: 5
 * Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
 *
 *
 * Note:
 *
 * All of the nodes' values will be unique.
 * p and q are different and both values will exist in the binary tree.
 *
 */
public class LowestCommonAncestorOfABinaryTree {

    // approach 1 : print the path of p and q | dfs | backtrack | O(N)
    // approach 2 : beautiful recursion | O(N)

    // approach 1:
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {

        // find path of p and q
        List<List<TreeNode>> pathList = new ArrayList<>();
        getPath(root, p, q, pathList, new ArrayList<>());

        // find the lowest of common node in two paths
        if (pathList.size() == 1) {
            if (pathList.get(0).contains(p)) {
                return p;
            } else {
                return q;
            }
        }
        for (int i = pathList.get(0).size() - 1; i >= 0; i--) {
            if (pathList.get(1).contains(pathList.get(0).get(i))) {
                return pathList.get(0).get(i);
            }
        }
        return null;
    }

    private void getPath(TreeNode root, TreeNode p, TreeNode q, List<List<TreeNode>> result, List<TreeNode> path) {

        if (root != null) {
            path.add(root);
            if (root.val == p.val || root.val == q.val) {
                result.add(new ArrayList<>(path));
                return;
            }


            getPath(root.left, p, q, result, path);
            if (root.left != null) {
                path.remove(path.size() - 1);
            }
            getPath(root.right, p, q, result, path);
            if (root.right != null) {
                path.remove(path.size() - 1);
            }

        }
    }

    // approach 2:
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root.val == p.val || root.val == q.val) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left == null) {
            return right;
        }
        if (right == null) {
            return left;
        }
        return root;
    }
}
 
发布了205 篇原创文章 · 获赞 27 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/huangdingsheng/article/details/104872362