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 (p.val > root.val && q.val > root.val) {
- return lowestCommonAncestor(root.right, p, q);
- } else if (p.val < root.val && q.val < root.val) {
- return lowestCommonAncestor(root.left, p, q);
- } else {
- return root;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement