Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
- if(root==null || root==p || root==q) return root;
- TreeNode L = lowestCommonAncestor(root.left,p,q);
- TreeNode R = lowestCommonAncestor(root.right,p,q);
- if(L!=null && R!=null) return root;
- if(L==null) return R;
- else return L;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement