LeetCode 题解(249) : Closest Binary Search Tree Value
来源:互联网 发布:苹果6手机壳淘宝 编辑:程序博客网 时间:2024/05/01 08:40
题目:
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.
递归。用全局变量保存当前最接近的节点值。
C++版:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int closestValue(TreeNode* root, double target) { if(root->val == target) return target; double cur = abs(root->val - target); if(cur < curM) { curM = cur; rootV = root->val; } if(root->val < target) { if(root->right != NULL) return closestValue(root->right, target); else return rootV; } else { if(root->left != NULL) return closestValue(root->left, target); else return rootV; } } private: double rootV = INT_MAX; double curM = std::numeric_limits<double>::max();};
Java版:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int closestValue(TreeNode root, double target) { if(root.val == target) return root.val; double cur = Math.abs(root.val - target); if(cur < curM) { rootV = root.val; curM = cur; } if(root.val > target) { if(root.left != null) return closestValue(root.left, target); else return rootV; } else { if(root.right != null) return closestValue(root.right, target); else return rootV; } } private int rootV = Integer.MAX_VALUE; private double curM = Double.MAX_VALUE;}
Python版:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneimport sysclass Solution(object): def __init__(self): self.v = sys.maxint self.curM = sys.maxint def closestValue(self, root, target): """ :type root: TreeNode :type target: float :rtype: int """ if root.val == target: return root.val cur = abs(root.val - target) if cur < self.curM: self.curM = cur self.v = root.val if root.val > target: if root.left != None: return self.closestValue(root.left, target) else: return self.v else: if root.right != None: return self.closestValue(root.right, target) else: return self.v
0 0
- LeetCode 题解(249) : Closest Binary Search Tree Value
- LeetCode 题解(250) : Closest Binary Search Tree Value II
- [Leetcode]Closest Binary Search Tree Value II
- [Leetcode]Closest Binary Search Tree Value
- leetcode 270: Closest Binary Search Tree Value
- *LeetCode-Closest Binary Search Tree Value
- [leetcode 270] Closest Binary Search Tree Value
- Leetcode 270Closest Binary Search Tree Value
- LeetCode 270: Closest Binary Search Tree Value
- LeetCode 270. Closest Binary Search Tree Value
- Leetcode 270 Closest Binary Search Tree Value
- Closest Binary Search Tree Value
- Closest Binary Search Tree Value
- leetcode 272: Closest Binary Search Tree Value II
- Leetcode NO.270 Closest Binary Search Tree Value
- [leetcode] 272. Closest Binary Search Tree Value II 解题报告
- LeetCode 272. Closest Binary Search Tree Value II
- [leetcode] 270. Closest Binary Search Tree Value 解题报告
- maxHappiness
- 设计模式学习笔记之迪米特法则
- PAT-A 1006. Sign In and Sign Out (25)
- [LeetCode 188] Best Time to Buy and Sell Stock IV
- id instancetype 区别
- LeetCode 题解(249) : Closest Binary Search Tree Value
- exits和In的区别(http://hi.baidu.com/delphi_relive/blog/item/d7c0034a49c4932208f7ef21.html)
- PHP实现验证码
- UPlayer播放器问题相关
- Linux tcpdump命令详解
- 多态 特征
- PHAsset心得
- bc57.1
- java中的String作为参数引起的一些研究