Thursday, May 28, 2015

L Question: LCA

public static TreeNode lca(TreeNode root, TreeNode p, TreeNode q) {
TreeNode left=null, right =null;
if (root == null) return null;
if (root==p || root==q) return root;
left = lca(root.left, p, q);
right = lca(root.right, p, q);

if (left!=null && right != null) return root;
return (left!=null)?left:right;

}

No comments:

Post a Comment