给定一个二叉树根节点,请你判断这棵树是不是二叉搜索树。
二叉搜索树满足每个节点的左子树上的所有节点均小于当前节点且右子树上的所有节点均大于当前节点。
例:
图1
图2
数据范围:节点数量满足 ,节点上的值满足
static List<Integer> list=new ArrayList<>(); public boolean isValidBST (TreeNode root) { InOrder(root); for(int i=0;i<list.size()-1;i++){ if(list.get(i)>list.get(i+1))return false; } return true; } void InOrder(TreeNode root){ if(root!=null){ InOrder(root.left); list.add(root.val); InOrder(root.right); } }
public class Solution { /** * 中序遍历,pre为前置的节点,如果存在 pre.val>cur.val 则返回false * * @param root TreeNode类 * @return bool布尔型 */ TreeNode pre = null; boolean result = true; public boolean isValidBST(TreeNode root) { inOrder(root); return result; } public void inOrder(TreeNode root) { if (root == null) return; inOrder(root.left); if (pre == null) { pre = root; return; } if (pre.val > root.val) { result = false; return; } pre = root; inOrder(root.right); } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ public boolean isValidBST (TreeNode root) { // write code here Stack<TreeNode> stack = new Stack<>(); int prev = Integer.MIN_VALUE; while(!stack.isEmpty() || root != null){ while(root != null){ stack.push(root); root = root.left; } if(!stack.isEmpty()){ root = stack.pop(); if(root.val <= prev) { return false; // 破坏了BST中序遍历的单调递增性质,直接返回false } prev = root.val; root = root.right; } } return true; // 没有发现单调性被破坏,是BST } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ public boolean isValidBST (TreeNode root) { // write code here int prev = Integer.MIN_VALUE; TreeNode cur = root; TreeNode mostRight = null; while(cur != null){ mostRight = cur.left; if(mostRight != null){ // 到左子树的最右节点 while(mostRight.right != null && mostRight.right != cur) { mostRight = mostRight.right; } if(mostRight.right == null){ // 第一次到左子树的最右节点,需要将其右孩子的指向指向cur mostRight.right = cur; cur = cur.left; continue; }else{ if(cur.val <= prev) { return false; } prev = cur.val; // 第二次到这要恢复右孩子的指针 mostRight.right = null; } }else{ // 只会到一次的节点,直接判断 if(cur.val <= prev){ return false; } prev = cur.val; } // 没有左子树节点直接右移 cur = cur.right; } return true; } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ static int min = Integer.MIN_VALUE; public boolean isValidBST (TreeNode root) { // write code here if(root == null){ return true; } if( !(isValidBST(root.left)) ){ return false; } if(root.val <= min){ return false; }else{ min = root.val; } if( !(isValidBST(root.right)) ){ return false; } return true; } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ static int min = Integer.MIN_VALUE; public boolean isValidBST (TreeNode root) { // write code here if(root == null){ return true; } // Stack<TreeNode> stack = new Stack<TreeNode>(); // while(! stack.isEmpty() || root!=null){ // //入栈向左一直走 // if(root != null){ // stack.push(root); // root = root.left; // }else{ // //访问右子树 // root = stack.pop(); // if(root.val <= min){ // return false; // }else{ // min = root.val; // root = root.right; // } // } // } if( !(isValidBST(root.left)) ){ return false; } if(root.val <= min){ return false; }else{ min = root.val; } if( !(isValidBST(root.right)) ){ return false; } return true; } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ bool isValidBST(TreeNode* root) { // 根节点没有约束 return isValidBSTTree(root, nullptr, nullptr); } // 从上到下验证每个节点是否符合该二叉搜索树的约束【min < root < max】 bool isValidBSTTree(TreeNode* root, TreeNode* min, TreeNode* max){ if(root == nullptr) return true; // 验证当前节点 if(min != nullptr && min->val >= root->val) return false; if(max != nullptr && max->val <= root->val) return false; // 验证左右子树【约束也需要更新】 return isValidBSTTree(root->left, min, root) && isValidBSTTree(root->right, root, max); } };
class Solution { //中序遍历法,迭代 public: bool isValidBST(TreeNode* root) { stack<TreeNode*> st; TreeNode* cur = root; TreeNode* last = nullptr; while (cur || !st.empty()) { if (cur) { st.push(cur); cur = cur->left; } else { cur = st.top(); st.pop(); if (last) { if (cur->val <= last->val) return false; } last = cur; cur = cur->right; } } return true; } };
private int min = Integer.MIN_VALUE; public boolean isValidBST (TreeNode root) { // write code here if (root == null) return true; //处理左子树 if (!isValidBST(root.left)) { return false; } //处理当前节点 if (min >= root.val) { return false; } else { min = root.val; } //处理右子树 if (!isValidBST(root.right)) { return false; } return true; }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ TreeNode pre; public boolean isValidBST (TreeNode root) { //左边如何 if(root.left!=null&&!isValidBST(root.left)){ return false; } //当前如何 if(pre!=null&&root.val<pre.val){ return false; } pre=root; //右边如何 if(root.right!=null){ return isValidBST(root.right); } return true; } }
public boolean isValidBST (TreeNode root) { // write code here return DFS(root,Integer.MIN_VALUE,Integer.MAX_VALUE); } // 满足 l < root.val< r public boolean DFS(TreeNode root,int l,int r){ if(root == null) return true; if(root.val < l || root.val > r) return false; return DFS(root.left,l,root.val) && DFS(root.right,root.val,r); }
// 辅助变量,pre指向前一个节点的值,初始值为Integer.MIN_VALUE,也就是说当第一次比较时 pre一定比 root.val小 Integer pre = Integer.MIN_VALUE; public boolean isValidBST (TreeNode root) { // write code here // 等于空,则返回true if(root == null) return true; // 向左递归,如果返回false 就return false if(!isValidBST (root.left)) return false; // 当前一个值pre 大于 当前节点的值 就返回false if(pre > root.val) return false; // 否则将前一个值修改为当前节点的值, pre = root.val; // 最后开始向右递归,并返回结果即可 return isValidBST (root.right); }
public boolean isValidBST (TreeNode root) { ArrayList<Integer>list = new ArrayList<>(); inorder(root,list); for(int i = 0;i<list.size()-1;i++){ if(list.get(i)>list.get(i+1)){ return false; } } return true; } public void inorder(TreeNode root,ArrayList<Integer>list){ if(root==null){ return; } inorder(root.left,list); list.add(root.val); inorder(root.right,list); }
static bool isValidBST_helper(TNode *root, int lower, int upper) { if (root == NULL) return true; int val = root->val; if (val > upper||val < lower) return false; return isValidBST_helper(root->left, lower,val)&&isValidBST_helper(root->right, val,upper); } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @param root TreeNode类 * @return bool */ bool isValidBST(struct TreeNode* root) { if (root == NULL) return true; return isValidBST_helper(root, INT32_MIN+1, INT32_MAX-1); }
class Solution: def inOrderTraversal(self, root: TreeNode) -> List[int]: if root is None: return [] return self.inOrderTraversal(root.left) + [root.val] + self.inOrderTraversal(root.right) def isValidBST(self , root: TreeNode) -> bool: res = self.inOrderTraversal(root) if len(res) <= 1: return True for i in range(len(res)-1): if res[i] >= res[i+1]: return False return True
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ public boolean isValidBST (TreeNode root) { // write code here if(root==null || (root.left==null && root.right==null)){ return true; } //list用来接收中序遍历的结果 ArrayList<Integer> list=new ArrayList<>(); infixOrder(list,root); //如果集合按照从小到大的顺序排列,则返回true for(int i=1;i<list.size();i++){ if(list.get(i-1)>list.get(i)){ return false; } } return true; } //中序遍历 public void infixOrder(ArrayList<Integer> list,TreeNode root){ if(root==null){ return; } infixOrder(list,root.left); list.add(root.val); infixOrder(list,root.right); } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ bool isValidBST(TreeNode* root) { // 时间复杂度O(N),空间复杂度O(N) if (root == nullptr) return true; if (!isValidBST(root->left) || root->val < pre) return false; pre = root->val; return isValidBST(root->right); } int pre = INT_MIN; };
ArrayList<Integer> list = new ArrayList<>(); public boolean isValidBST (TreeNode root) { // write code here ArrayList<Integer> list1 = inorder(root); int[] ret = new int[list1.size()]; for(int i = 1 ; i < list1.size(); i++){ if(ret[i-1] > ret[i]) return false; } return true; } ArrayList<Integer> inorder(TreeNode node){ if(node == null) return list; inorder(node.left); list.add(node.val); inorder(node.right); return list; } //佬 救救孩子 孩子用中序遍历 然后判断 为什么不行呀
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ //二叉搜索树 按照中序遍历 是有序的 long long pre=INT_MIN; bool isValidBST(TreeNode* root) { // write code here if(root==NULL){ return true; } if(!isValidBST(root->left)){ return false; } if(pre>root->val){ return false; } pre=root->val; if(!isValidBST(root->right)){ return false; } return true; } };