My java solution:)


  • 13
    Z
    /**
     * 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(true){
              if((root.val-p.val)*(root.val-q.val)<=0) return root;
              if(p.val<root.val){
                 root=root.left;
              }
             else{
                  root=root.right;
              }
            }
        }
    }

  • 0
    This post is deleted!

  • 0
    Z

    thanks, this is the first time I try to post mu solution,sorry for the confusion~


  • 0
    L

    un-fucking-believable !!


  • 0
    N

    While this may pass the online judge, I would recommend null checks for all the arguments in practice. If this was an interview the asker might want to see that.


  • 0
    M

    This is a good solution.but you don't check null.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.