[Algorithm] BST问题
来源:互联网 发布:直播app数据库设计 编辑:程序博客网 时间:2024/05/20 17:59
BST性质:左边都比root小,右边都比root大
BST的中序遍历得到的节点访问顺序是从小到大的顺序
98. Valid Binary Search Tree: 点击打开链接
方法一
思路:先中序遍历BST拿到结果list,遍历list里元素,看当前的元素值是不是小于下一个元素值,只要有当前元素值大于等于下一个元素值的,就返回false
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root: The root of binary tree. * @return: True if the binary tree is BST, or false */ public boolean isValidBST(TreeNode root) { if(root==null){ return true; } List<TreeNode> result=inOrder(root); for(int i=0;i<result.size()-1;i++){ if(result.get(i).val>=result.get(i+1).val){ return false; } } return true; } private List<TreeNode> inOrder(TreeNode root){ List<TreeNode> list=new ArrayList<>(); Stack<TreeNode> stack=new Stack<>(); TreeNode cur=root; while(cur!=null || !stack.isEmpty()){ while(cur!=null){ stack.push(cur); cur=cur.left; } cur=stack.pop(); list.add(cur); cur=cur.right; } return list; }}方法二
思路:递归,边遍历边比较
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isValidBST(TreeNode root) { return helper(root,Long.MAX_VALUE, Long.MIN_VALUE); } public boolean helper(TreeNode root,long maxVal,long minVal){ if(root==null){ return true; } if(root.val>=maxVal || root.val<=minVal){ return false; } return helper(root.left,root.val,minVal) && helper(root.right,maxVal,root.val); } }
11. Search Range in Binary Search Tree: 点击打开链接
思路:中序遍历得到list,再遍历拿到list里每一个符合条件的值
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root: The root of the binary search tree. * @param k1 and k2: range k1 to k2. * @return: Return all keys that k1<=key<=k2 in ascending order. */ public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) { ArrayList<Integer> result=new ArrayList<>(); if(root==null){ return result; } ArrayList<Integer> list=inorder(root); for(int i=0;i<list.size();i++){ if(list.get(i)>=k1 && list.get(i)<=k2){ result.add(list.get(i)); } } return result; } private ArrayList<Integer> inorder(TreeNode root){ ArrayList<Integer> result=new ArrayList<>(); if(root==null){ return result; } Stack<TreeNode> stack=new Stack<>(); TreeNode cur=root; while(!stack.isEmpty() || cur!=null){ while(cur!=null){ stack.push(cur); cur=cur.left; } cur=stack.pop(); result.add(cur.val); cur=cur.right; } return result; }}
173.Binary Search Tree Iterator: 点击打开链接
思路:就是BST中序遍历的拆解形式
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class BSTIterator { Stack<TreeNode> stack; public BSTIterator(TreeNode root) { stack=new Stack<>(); AddNodeToStack(root); } /** @return whether we have a next smallest number */ public boolean hasNext() { return !stack.isEmpty(); } /** @return the next smallest number */ public int next() { TreeNode cur=stack.pop(); AddNodeToStack(cur.right); return cur.val; } private void AddNodeToStack(TreeNode root){ while(root!=null){ stack.push(root); root=root.left; } }}/** * Your BSTIterator will be called like this: * BSTIterator i = new BSTIterator(root); * while (i.hasNext()) v[f()] = i.next(); */
阅读全文
0 0
- [Algorithm] BST问题
- BST sorting algorithm
- Algorithm:BST(Binary Search Tree)构造
- O(n^2)-time Algorithm to OPTIMAL-BST Question
- Algorithm: Binary Tree(2) -- Special Trees (BST and Balanced Tree)
- 【Algorithm】棋盘覆盖问题
- Karatsuba algorithm乘法问题
- [Algorithm] Palindrome 问题
- [Algorithm]Quick Select问题
- [Algorithm] Combination问题
- [Algorithm] Rotate 问题
- BST
- BST
- BST
- BST
- BST
- BST
- bst
- <context:annotation-config/>,<mvc:annotation-driven/>和<context:component-scan>之间的关系
- [9] 正则表达式
- Euler筛法用于素数筛选
- 从CSS理解LESS
- Angular 指令
- [Algorithm] BST问题
- 使用Linq查询数据进行分页时遇到的性能问题
- java面试总结
- bzoj 3211: 花神游历各国 && bzoj 3038: 上帝造题的七分钟2
- Java并发编程二
- android 自定义控件的几种形式
- Textbook Ex.8.22
- MySQL设计原则
- Android 6.0动态权限和一些收获