跳过总结请点这里:
https://segmentfault.com/a/11...node
BST最明显的特色就是root.left.val < root.val < root.right.val.
还有另外一个特色就是,bst inorder traversal result is an ascending array.segmentfault
下面简单表示一个BST:app
60 / \ 40 80 / \ / \ 20 50 70 90 / \ \ 10 30 100
BST inorder traversal result is: 10 20 30 40 50 60 70 80 90 100.this
BST 和普通的树的区别就在于它是一个有序的,为了保证顺序,咱们须要经过inorder traversal获得,因此几乎全部leetcode关于bst的题目,都是在考咱们如何利用inorder traverse.code
简单题目只过代码思路,代码会附在题号底下,为了减小博客篇幅,题目描述细节请参看leetcode。element
LC 173 Binary Search Tree Iterator.
Calling next() will return the next smallest number in the BST.leetcode
题目意思就是要一个个的返回bst当前的最小值。强提示:有序。
因此解法天然就是bst inorder traverse。起点就是BST里最小的值,也就是leftmost.get
public class BSTIterator { ArrayDeque<TreeNode> stk; public BSTIterator(TreeNode root) { stk = new ArrayDeque<TreeNode>(); pushAll(root); } /** @return whether we have a next smallest number */ public boolean hasNext() { return !stk.isEmpty(); } /** @return the next smallest number */ public int next() { TreeNode node = stk.pop(); if(node.right !=null) { pushAll(node.right); } return node.val; } public void pushAll(TreeNode node){ while(node != null){ stk.push(node); node = node.left; } } }
新增了预处理的方法。这个方法的好处就是每次next()都是严格的O(1), 而上面那个方法只是AVE O(1).博客
public class BSTIterator { ArrayList<Integer> ascending; int pos = 0; public BSTIterator(TreeNode root) { ascending = new ArrayList<Integer>(); inorder(root, ascending); } /** @return whether we have a next smallest number */ public boolean hasNext() { return pos < ascending.size(); } /** @return the next smallest number */ public int next() { return ascending.get(pos++); } public void inorder(TreeNode root, ArrayList<Integer> ascending){ if(root == null) return; inorder(root.left, ascending); ascending.add(root.val); inorder(root.right, ascending); } }
LC 230 Kth Smallest Element in a BSTio
返回BST里第k小的元素。 强提示:有序。
从leftmost开始,找到第k个点返回。时间复杂度近似到O(K).
为了写代码的清晰度,咱们使用recursion的方法作inorder traversal。
public class Solution { public int kthSmallest(TreeNode root, int k) { if(root == null) return 0; int[] res = {Integer.MIN_VALUE}; kthSmallest(root, k, res); return res[0]; } public int kthSmallest(TreeNode root, int k, int[] res){ if(res[0] != Integer.MIN_VALUE || root == null){ //if finded kth, all recursion will immediately return to root. return 0; } int left = kthSmallest(root.left, k,res); if(left == k-1){ res[0] = root.val; } int right = kthSmallest(root.right, k - left -1, res); return left + right + 1;
LC99 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
若是BST中有两个节点位置互相交换了,怎么办? 代表BST的顺序被打乱。咱们须要找出被打乱的点并返回正确结果。
利用上面给的BST的图,咱们swap(20, 90),获得以下BST:
60 / \ 40 80 / \ / \ 90 50 70 20 / \ \ 10 30 100
inorder traversal result is:
10 90 30 40 50 60 70 80 20 100
this is not an ascending array.
90 >30, 80 >20 这两处顺序不合理,靠前的值大于靠后的值。
因此这里咱们在inorder traversal的同时,咱们还须要比较pre.val和cur.val。
而后将两个不正确的点记录下来,最后swap回原来正确的值。
public class Solution { private TreeNode firstNode = null; private TreeNode secondNode = null; private TreeNode preNode = new TreeNode(Integer.MIN_VALUE); public void recoverTree(TreeNode root) { if(root == null) return; inorderTraversal(root); int temp = firstNode.val; firstNode.val = secondNode.val; secondNode.val = temp; } public void inorderTraversal(TreeNode root){ if(root == null) return; inorderTraversal(root.left); if(firstNode == null && preNode.val > root.val) { firstNode = preNode; } if(firstNode != null && preNode.val > root.val) { secondNode = root; } preNode = root; inorderTraversal(root.right); } }
LC450 Delete Node in a BST
找到一个点容易,利用root.left.val < root.val < root.right.val,时间复杂度O(logN)找到。
删除一个点。若是是叶子节点,或者只有一个子树。都容易操做。
若是是中间节点怎么办?咱们找到它在bst里的前一个点(或者后一个点),改变要删除节点的值,而后删除它的前一个点。
思想来自于heap的代码实现。咱们每次Pop的时候取的是根节点的值,而后把heap里最尾端的点换到根节点,而后shift down。
public class Solution { public TreeNode deleteNode(TreeNode root, int key) { if(root == null) return null; if(key < root.val) { root.left = deleteNode(root.left, key); } else if(key > root.val) { root.right = deleteNode(root.right, key); } else { if(root.left == null) { return root.right; } else if(root.right == null) { return root.left; } TreeNode node = findNextMin(root.right); root.val = node.val; root.right = deleteNode(root.right, node.val); } return root; } public TreeNode findNextMin(TreeNode node) { while(node.left != null) { node = node.left; } return node; } }
写不下了,换一篇,点这里:
https://segmentfault.com/a/11...