Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
- while (root != null) {
- if (p.val < root.val && q.val < root.val) {
- root = root.left;
- } else if (p.val > root.val && q.val > root.val) {
- root = root.right;
- } else break;
- }
- return root;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement