@@ -0,0 +1,1582 @@
+package leetcode;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/%0A */%0Apublic class Problem235 %7B%0A    public static class TreeNode %7B%0A        int val;%0A        TreeNode left;%0A        TreeNode right;%0A%0A        TreeNode(int x) %7B%0A            val = x;%0A        %7D%0A    %7D%0A%0A    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) %7B%0A        List%3CTreeNode%3E path1 = getPath(root, p);%0A        List%3CTreeNode%3E path2 = getPath(root, q);%0A        int minSize = Math.min(path1.size(), path2.size());%0A        TreeNode lca = null;%0A        for (int i = 0; i %3C minSize; i++) %7B%0A            if (path1.get(i).val != path2.get(i).val) %7B%0A                break;%0A            %7D else %7B%0A                lca = path1.get(i);%0A            %7D%0A        %7D%0A        return lca;%0A    %7D%0A    %0A    private List%3CTreeNode%3E getPath(TreeNode root, TreeNode n) %7B%0A        List%3CTreeNode%3E result = new ArrayList%3C%3E();%0A        getPath(root, n, new ArrayList%3C%3E(), result);%0A        return result;%0A    %7D%0A%0A    private void getPath(TreeNode root, TreeNode n, List%3CTreeNode%3E path, List%3CTreeNode%3E result) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D%0A        if (root == n) %7B%0A            result.addAll(path);%0A            result.add(root);%0A            return;%0A        %7D%0A        List%3CTreeNode%3E newPath = new ArrayList%3C%3E(path);%0A        newPath.add(root);%0A        getPath(root.left, n, newPath, result);%0A        newPath = new ArrayList%3C%3E(path);%0A        newPath.add(root);%0A        getPath(root.right, n, newPath, result);%0A    %7D%0A%7D%0A

Solve problem 235