Advertisement
aero2146

Lowest Common Ancestor of a Binary Search Tree

Jan 27th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.39 KB | None | 0 0
  1. class Solution {
  2.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  3.         if (p.val > root.val && q.val > root.val) {
  4.             return lowestCommonAncestor(root.right, p, q);
  5.         } else if (p.val < root.val && q.val < root.val) {
  6.             return lowestCommonAncestor(root.left, p, q);
  7.         } else {
  8.             return root;
  9.         }
  10.     }
  11. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement