zoukankan      html  css  js  c++  java
  • 270. Closest Binary Search Tree Value

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.

    Note:

    • Given target value is a floating point.
    • You are guaranteed to have only one unique value in the BST that is closest to the target.

    本题我用了变形题2的思路来做的,时间用的太长了,代码如下:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int closestValue(TreeNode root, double target) {
    12         Stack<TreeNode> succ = new Stack<TreeNode>();
    13         Stack<TreeNode> pre = new Stack<TreeNode>();
    14         initalizesucc(succ,root,target);
    15         initalizepre(pre,root,target);
    16         if(succ.isEmpty()){
    17             return pre.peek().val;
    18         }else if(pre.isEmpty()){
    19             return succ.peek().val;
    20         }else{
    21             double succ_diff = Math.abs((double)succ.peek().val-target);
    22             double pre_diff = Math.abs((double)pre.peek().val-target);
    23             if(succ_diff<pre_diff) return succ.peek().val;
    24             else return pre.peek().val;
    25         }
    26     }
    27     public void initalizesucc(Stack<TreeNode> stack,TreeNode root,double target){
    28         while(root!=null){
    29             if(root.val==target){
    30                 stack.push(root);
    31                 break;
    32             }else if(root.val<target){
    33                 root = root.right;
    34             }else{
    35                 stack.push(root);
    36                 root = root.left;
    37             }
    38         }
    39     }
    40     public void initalizepre(Stack<TreeNode> stack,TreeNode root,double target){
    41         while(root!=null){
    42             if(root.val==target){
    43                 stack.push(root);
    44                 break;
    45             }else if(root.val<target){
    46                 stack.push(root);
    47                 root = root.right;
    48             }else{
    49                 root = root.left;
    50             }
    51         }
    52     }
    53 }

    本题还有更为简单的办法,思路是使用递归来做,找出与target左面和右面最近的两个值,返回差值较小者。代码如下:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int closestValue(TreeNode root, double target) {
    12         int a = root.val;
    13         TreeNode child = target<a?root.left:root.right;
    14         if(child==null) return a;
    15         int b = closestValue(child,target);
    16         return Math.abs(a-target)<Math.abs(b-target)?a:b;
    17     }
    18 }
  • 相关阅读:
    C#中的多态
    反编译工具
    富文本粘贴图片
    [Silverlight入门系列]用TransformToVisual和Transform取得元素绝对位置(Location)
    Silverlight在IIS中的配置
    HubbleDotNet开源全文搜索数据库项目技术详解
    Thank you for choosing Telerik RadRichTextBox
    Asp.net读取AD域信息的方法<转>
    Sharepoint学习笔记—ECMAScript对象模型系列1、ECMAScript对象模型的引入
    SharePoint WebService
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6609277.html
Copyright © 2011-2022 走看看