java no recursive


  • 0
    J

    /235. Lowest Common Ancestor of a Binary Search Tree/
    public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    //非递归解法,while true
    while(true){
    if(root.val > p.val && root.val > q.val){
    root = root.left;
    }else if(root.val < p.val && root.val < q.val){
    root = root.right;
    }else{
    return root;
    }
    }
    }
    }


Log in to reply
 

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