package leetcode;

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

/**
 * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
 */
public class Problem235 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        List<TreeNode> path1 = getPath(root, p);
        List<TreeNode> path2 = getPath(root, q);
        int minSize = Math.min(path1.size(), path2.size());
        TreeNode lca = null;
        for (int i = 0; i < minSize; i++) {
            if (path1.get(i).val != path2.get(i).val) {
                break;
            } else {
                lca = path1.get(i);
            }
        }
        return lca;
    }
    
    private List<TreeNode> getPath(TreeNode root, TreeNode n) {
        List<TreeNode> result = new ArrayList<>();
        getPath(root, n, new ArrayList<>(), result);
        return result;
    }

    private void getPath(TreeNode root, TreeNode n, List<TreeNode> path, List<TreeNode> result) {
        if (root == null) {
            return;
        }
        if (root == n) {
            result.addAll(path);
            result.add(root);
            return;
        }
        List<TreeNode> newPath = new ArrayList<>(path);
        newPath.add(root);
        getPath(root.left, n, newPath, result);
        getPath(root.right, n, newPath, result);
    }
}

@@ -1394,32 +1394,69 @@
 Path.add(root);%0A
+        if (root.val %3E= n.val) %7B%0A    
         getPath(
@@ -1479,32 +1479,53 @@
 wPath, result);%0A
+        %7D else %7B%0A    
         getPath(
@@ -1553,20 +1553,30 @@
 h, result);%0A
+        %7D%0A
     %7D%0A%7D%0A

Update problem 235 solution