@@ -0,0 +1,1427 @@
+/**%0A * Definition for a binary tree node.%0A * public class TreeNode %7B%0A *     int val;%0A *     TreeNode left;%0A *     TreeNode right;%0A *     TreeNode(int x) %7B val = x; %7D%0A * %7D%0A */%0Apublic class Solution %7B%0A    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) %7B%0A        if(root == null %7C%7C p == null %7C%7C q == null)%7B%0A            return null;%0A        %7D%0A        Stack%3CTreeNode%3E pStack = new Stack%3CTreeNode%3E();%0A        Stack%3CTreeNode%3E qStack = new Stack%3CTreeNode%3E();%0A        TreeNode result = null;%0A        dfs(root, p, pStack);%0A        dfs(root, q, qStack);%0A        if(pStack.isEmpty() %7C%7C qStack.isEmpty())%7B%0A            return null;%0A        %7D%0A        while(!pStack.isEmpty() && !qStack.isEmpty())%7B%0A            TreeNode pTmp = pStack.pop();%0A            TreeNode qTmp = qStack.pop();%0A            if(pTmp == qTmp)%7B%0A                result = pTmp;%0A            %7Delse%7B%0A                break;%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A    %0A    private boolean dfs(TreeNode root, TreeNode node, Stack%3CTreeNode%3E stack)%7B%0A        if(root == null)%7B%0A            return false;%0A        %7Delse if(root == node)%7B%0A            stack.push(root);%0A            return true;%0A        %7D%0A        if(dfs(root.left, node, stack))%7B%0A            stack.push(root);%0A            return true;%0A        %7Delse if(dfs(root.right, node, stack))%7B%0A            stack.push(root);%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A    %0A%7D

Q236 Lowest Common Ancestor of a Binary Tree