LeetCode 刷题【98. 验证二叉搜索树】
98. 验证二叉搜索树
自己做
解:中序遍历
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {private boolean isBST = true;private boolean isStartBorder_left = true; //记录边界值是初始的还是结点的(针对最大最小值边界)private boolean isStartBorder_right = true; //记录边界值是初始的还是结点的(针对最大最小值边界)public void midTravel(TreeNode root, int left, int right){if(root == null)return;midTravel(root.left, left, root.val); //往左遍历if(root.left != null && root.val <= root.left.val || //左孩子存在的情况下,考虑是否大于左孩子root.left == null && root.val <= left && root.val != Integer.MIN_VALUE //左孩子不存在的情况下考虑边界){isBST = false;return;}if(root.right != null && root.val >= root.right.val || //右孩子存在的情况下,考虑是否小于右孩子root.right == null && root.val >= right && root.val != Integer.MAX_VALUE //右孩子不存在的情况下考虑边界){isBST = false;return;}//等于初始边界的情况if(root.val == Integer.MIN_VALUE){if(left != Integer.MIN_VALUE){ //左边界存在(不是初始边界)的情况下,必然不是BSTisBST = false;return;}else{ //左边界不存在的情况下考虑是否为初始边界if(isStartBorder_left) //如果是对应初始边界=》无事发生isStartBorder_left = false;else{ //不是初始边界,说明有两个最小值,不是BSTisBST = false;return;}} }if(root.val == Integer.MAX_VALUE){if(right != Integer.MAX_VALUE){ //右边界存在(不是初始边界)的情况下,必然不是BSTisBST = false;return;}else{ //右边界不存在的情况下考虑是否为初始边界if(isStartBorder_right) //如果是对应初始边界=》无事发生isStartBorder_right = false;else{ //不是初始边界,说明有两个最大值,不是BSTisBST = false;return;}} }midTravel(root.right, root.val, right); //往右遍历}public boolean isValidBST(TreeNode root) {midTravel(root, Integer.MIN_VALUE, Integer.MAX_VALUE);return isBST;}
}
99. 恢复二叉搜索树
自己做(歇菜)
看题解
https://leetcode.cn/problems/recover-binary-search-tree/solutions/271778/san-chong-jie-fa-xiang-xi-tu-jie-99-hui-fu-er-cha-
class Solution {public void recoverTree(TreeNode root) {List<TreeNode> list = new ArrayList<TreeNode>();dfs(root,list);TreeNode x = null;TreeNode y = null;//扫面遍历的结果,找出可能存在错误交换的节点x和yfor(int i=0;i<list.size()-1;++i) {if(list.get(i).val>list.get(i+1).val) {y = list.get(i+1);if(x==null) {x = list.get(i);}}}//如果x和y不为空,则交换这两个节点值,恢复二叉搜索树if(x!=null && y!=null) {int tmp = x.val;x.val = y.val;y.val = tmp;}}//中序遍历二叉树,并将遍历的结果保存到list中 private void dfs(TreeNode node,List<TreeNode> list) {if(node==null) {return;}dfs(node.left,list);list.add(node);dfs(node.right,list);}
}作者:王尼玛
链接:https://leetcode.cn/problems/recover-binary-search-tree/solutions/271778/san-chong-jie-fa-xiang-xi-tu-jie-99-hui-fu-er-cha-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
神之领域
class Solution {public void recoverTree(TreeNode root) {if(root==null) {return;}TreeNode x = null;TreeNode y = null;TreeNode pre = null;TreeNode tmp = null;while(root!=null) {if(root.left!=null) {tmp = root.left;while(tmp.right!=null && tmp.right!=root) {tmp = tmp.right;}if(tmp.right==null) {tmp.right = root;root = root.left;}else {if(pre!=null && pre.val>root.val) {y = root;if(x==null) {x = pre;}}pre = root;tmp.right = null;root = root.right;}}else {if(pre!=null && pre.val>root.val) {y = root;if(x==null) {x = pre;}}pre = root;root = root.right;}}if(x!=null && y!=null) {int t = x.val;x.val = y.val;y.val = t;}}
}作者:王尼玛
链接:https://leetcode.cn/problems/recover-binary-search-tree/solutions/271778/san-chong-jie-fa-xiang-xi-tu-jie-99-hui-fu-er-cha-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。