Cloest Binary Search Tree Value II

 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 List<Integer> closestKValues(TreeNode root, double target, int k) {
12         List<Integer> result = new ArrayList<>();
13         if (k == 0 || root == null) {
14             return result;
15         }
16
17         Stack<TreeNode> successor = new Stack<>();
18         Stack<TreeNode> precessor = new Stack<>();
19
20         initialStack(successor, root, true, target);
21         initialStack(precessor, root, false, target);
22
23         if(!successor.isEmpty() && !precessor.isEmpty() && successor.peek().val == precessor.peek().val) {
24             getNext(precessor, false);
25         }
26
27         while (result.size() < k) {
28             if (successor.isEmpty()) {
29                 result.add(getNext(precessor, false));
30             } else if (precessor.isEmpty()) {
31                 result.add(getNext(successor, true));
32             } else {
33                 boolean sclose = Math.abs((double) successor.peek().val - target) < Math.abs((double)precessor.peek().val - target);
34                 if (sclose) {
35                     result.add(getNext(successor, true));
36                 } else {
37                     result.add(getNext(precessor, false));
38                 }
39             }
40         }
41         return result;
42     }
43
44     private void initialStack(Stack<TreeNode> stack, TreeNode root, boolean successor, double target) {
45         while (root != null) {
46             if (root.val == target) {
47                 stack.push(root);
48                 break;
49             } else if (root.val > target == successor) {
50                 stack.push(root);
51                 root = successor ? root.left : root.right;
52             } else {
53                 root = successor ? root.right : root.left;
54             }
55         }
56     }
57
58     private int getNext(Stack<TreeNode> stack, boolean successor) {
59         TreeNode current = stack.pop();
60         int result = current.val;
61         current = successor ? current.right : current.left;
62         while (current != null) {
63             stack.push(current);
64             current = successor ? current.left : current.right;
65         }
66         return result;
67     }
68 }
时间: 2024-08-25 19:18:47

Cloest Binary Search Tree Value II的相关文章

272. Closest Binary Search Tree Value II

/* * 272. Closest Binary Search Tree Value II * 2016-6-25 by Mingyang * 一开始这个题目我想用priority queue来做,就是把所有的stack里面的全部加进来 * 然后一个一个的找 */ public List<Integer> closestKValues1(TreeNode root, double target, int k) { Stack<TreeNode> stack = new Stack&

[LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have onl

[?]*Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have onl

LeetCode-Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have onl

[LeetCode#272] Closest Binary Search Tree Value II

Problem: Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to

LC 272. Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have onl

LeetCode Closest Binary Search Tree Value

原题链接在这里:https://leetcode.com/problems/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

[LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这道题是要求把有序链表转为二叉搜索树,和之前那道Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树思路完全一样,只不过是操作的数据类型有所差别,一个是数组,一个是链表.数组方便就方便在可以通过index直接访问任意一个元

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has