@@ -0,0 +1,1473 @@
+package leetcode;%0A%0Aimport java.util.Stack;%0A%0A/**%0A * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/%0A */%0Apublic class Problem236 %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        Stack%3CTreeNode%3E path1 = getPath(root, p);%0A        Stack%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            TreeNode node1 = path1.pop();%0A            TreeNode node2 = path2.pop();%0A            if (node1.val != node2.val) %7B%0A                break;%0A            %7D else %7B%0A                lca = node1;%0A            %7D%0A        %7D%0A        return lca;%0A    %7D%0A    %0A    private Stack%3CTreeNode%3E getPath(TreeNode root, TreeNode n) %7B%0A        if (root == null) %7B%0A            return null;%0A        %7D%0A        if (root == n) %7B%0A            Stack%3CTreeNode%3E path = new Stack%3C%3E();%0A            path.add(root);%0A            return path;%0A        %7D%0A        Stack%3CTreeNode%3E result = getPath(root.left, n);%0A        if (result != null) %7B%0A            result.add(root);%0A            return result;%0A        %7D%0A        result = getPath(root.right, n);%0A        if (result != null) %7B%0A            result.add(root);%0A            return result;%0A        %7D%0A        return result;%0A    %7D%0A%7D%0A

Solve problem 236