Advertisement
unknown_0711

Untitled

Nov 28th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. class Solution {
  2. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  3. if(root==null || root==p || root==q) return root;
  4. TreeNode L = lowestCommonAncestor(root.left,p,q);
  5. TreeNode R = lowestCommonAncestor(root.right,p,q);
  6. if(L!=null && R!=null) return root;
  7. if(L==null) return R;
  8. else return L;
  9. }
  10. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement