LC刷题4/5 - Top FB Questions - M270/ M973

4/5 Facebook Medium Questions

270. Closest Binary Search Tree Value

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int closestValue(TreeNode root, double target) {
        return (int) iterValue(root, target);
    }
    
    
    public double iterValue(TreeNode root, double target) {
        double iterVal = -1; 
        if (root == null) 
            return Double.MAX_VALUE;
        if (target < root.val) {
            iterVal = iterValue(root.left, target);
        } else if (target > root.val) {
            iterVal = iterValue(root.right, target);
        } else {
            iterVal = target;
        }
        
        if (Math.abs(iterVal-target) > Math.abs(root.val-target)) {
            iterVal = root.val;
        }
        
        return iterVal;
    }
}

Solution

Approach 1: Recursive Inorder + Linear search, O(N) time
class Solution {
  public void inorder(TreeNode root, List<Integer> nums) {
    if (root == null) return;
    inorder(root.left, nums);
    nums.add(root.val);
    inorder(root.right, nums);
  }

  public int closestValue(TreeNode root, double target) {
    List<Integer> nums = new ArrayList();
    inorder(root, nums);
    return Collections.min(nums, new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
        return Math.abs(o1 - target) < Math.abs(o2 - target) ? -1 : 1;
      }
    });
  }
}

Complexity Analysis

Time complexity : O(N)\mathcal{O}(N)O(N) because to build inorder traversal and then to perform linear search takes linear time.
Space complexity : O(N)\mathcal{O}(N)O(N) to keep inorder traversal. 
Approach 2: Iterative Inorder, O(k) time
class Solution {
  public int closestValue(TreeNode root, double target) {
    LinkedList<TreeNode> stack = new LinkedList();
    long pred = Long.MIN_VALUE;

    while (!stack.isEmpty() || root != null) {
      while (root != null) {
        stack.add(root);
        root = root.left;
      }
      root = stack.removeLast();

      if (pred <= target && target < root.val)
        return Math.abs(pred - target) < Math.abs(root.val - target) ? (int)pred : root.val;

      pred = root.val;
      root = root.right;
    }
    return (int)pred;
  }
}
Approach 3: Binary Search, O(H) time
class Solution {
  public int closestValue(TreeNode root, double target) {
    int val, closest = root.val;
    while (root != null) {
      val = root.val;
      closest = Math.abs(val - target) < Math.abs(closest - target) ? val : closest;
      root =  target < root.val ? root.left : root.right;
    }
    return closest;
  }
}

Complexity Analysis

Time complexity : O(H)\mathcal{O}(H)O(H) since here one goes from root down to a leaf.

Space complexity : O(1)\mathcal{O}(1)O(1).

973. K Closest Points to Origin

在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值