SHARE
TWEET

Untitled

a guest Apr 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. boolean found = false;
  2. public TreeNode helper(TreeNode root, TreeNode p, TreeNode q)
  3. {
  4.     if(found||root==null) return null;
  5.     TreeNode left = helper(root.left, p, q);
  6.     TreeNode right = helper(root.right, p, q);
  7.    
  8.     if(left!=null&&right!=null)
  9.     {
  10.         found = true;
  11.         return root;
  12.     }
  13.     if(root.val==p.val||root.val==q.val)
  14.         return root;
  15.     else if(left!=null)
  16.         return left;
  17.     else if(right!=null)
  18.         return right;
  19.    
  20.     return null;
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top