随笔分类 -  bst

701. Insert into a Binary Search Tree
摘要:Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that t... 阅读全文

posted @ 2018-11-08 16:24 猪猪🐷

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 floati 阅读全文

posted @ 2018-11-08 16:03 猪猪🐷

220. Contains Duplicate III
摘要:Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference... 阅读全文

posted @ 2018-11-08 02:14 猪猪🐷

333. Largest BST Subtree
摘要:Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:
A subtree must include all of its descendants.... 阅读全文

posted @ 2018-11-07 05:11 猪猪🐷

501. Find Mode in Binary Search Tree
摘要:Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: * The left subtree of a node contai... 阅读全文

posted @ 2018-11-07 05:08 猪猪🐷

783. Minimum Distance Between BST Nodes
摘要:Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output... 阅读全文

posted @ 2018-11-07 05:07 猪猪🐷

776. Split BST
摘要:Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the o... 阅读全文

posted @ 2018-11-07 05:06 猪猪🐷

538. Convert BST to Greater Tree
摘要:Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Exampl... 阅读全文

posted @ 2018-11-07 05:06 猪猪🐷

255. Verify Preorder Sequence in Binary Search Tree
摘要:Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Consider the following binary se... 阅读全文

posted @ 2018-11-07 05:05 猪猪🐷

669. Trim a Binary Search Tree
摘要:Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the resu... 阅读全文

posted @ 2018-11-07 05:05 猪猪🐷

inorder successor bst
摘要:Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in 阅读全文

posted @ 2018-09-08 04:35 猪猪🐷

inorder predecessor bst
摘要:Bst inorder successor/ predecessore With parent pointer, without parent pointer Iterative, recursive ======================================= Predecessor with a parent pointer (iterative) idea: ... 阅读全文

posted @ 2018-09-08 04:34 猪猪🐷

99. Recover Binary Search Tree
摘要:Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2 阅读全文

posted @ 2018-08-28 20:43 猪猪🐷

108. Convert Sorted Array to balanced Binary Search Tree
摘要:带返回值的 recursion Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced 阅读全文

posted @ 2018-08-16 23:54 猪猪🐷

binary search tree to sorted double linked list (not circular) (okay)
摘要:TreeNode head = null; TreeNode prev = null; public void inOrder(TreeNode root){ if(root == null) return; inOrder(root.left); if(prev == null){ head = root; }else{ prev.right = root... 阅读全文

posted @ 2018-08-16 23:52 猪猪🐷

109. 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. For this problem, a height-balanced bina 阅读全文

posted @ 2018-08-16 23:51 猪猪🐷

Convert Sorted double linked List to Binary Search Tree
摘要:把自己写的solution 1 和 闫老师写的solution2 都弄懂,会写。 bst变double linked list。recursion秒了,然后第一个follow up是把双链表变回去,要求balance。第二个follow up是在牺牲空间复杂度的情况下如何优化时间,想了个时间O(n) 阅读全文

posted @ 2018-08-16 23:49 猪猪🐷

653 two sum iv - input is a bst
摘要:653 two sum iv - input is a bst Approach #1 Using HashSet[Accepted] The simplest solution will be to traverse over the whole tree and consider every p 阅读全文

posted @ 2018-08-11 04:15 猪猪🐷

450. Delete Node in a BST
摘要:Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of th 阅读全文

posted @ 2018-08-09 18:31 猪猪🐷

Construct BST from preorder list
摘要:Given preorder traversal of a binary search tree, construct the BST. For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output sho 阅读全文

posted @ 2018-08-09 18:26 猪猪🐷

导航