Leetcode Validate Binary Search Tree
LeetCode Tree search Binary validate
2023-09-11 14:14:42 时间
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
confused what "{1,#,2,3}"
means? >
read more on how binary tree is serialized on OJ.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool check(TreeNode *node,int lv,int rv){ if(node==NULL) return true; return lv<node->val&&node->val<rv&&check(node->left,lv,node->val)&&check(node->right,node->val,rv); } bool isValidBST(TreeNode *root) { return check(root,INT_MIN,INT_MAX); } };
相关文章
- Leetcode: Find Leaves of Binary Tree
- Leetcode: Binary Tree Vertical Order Traversal
- Leetcode: Verify Preorder Sequence in Binary Search Tree
- Binary Search Tree 以及一道 LeetCode 题目
- [Leetcode] Convert Sorted List to Binary Search Tree
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Convert Sorted Array to Binary Search Tree
- LeetCode:Balanced Binary Tree
- [LeetCode] 1038. Binary Search Tree to Greater Sum Tree 二叉搜索树到较大和树
- [LeetCode] 623. Add One Row to Tree 二叉树中增加一行
- [LeetCode] 538. Convert BST to Greater Tree 将二叉搜索树BST转为较大树
- [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
- [LeetCode] 270. Closest Binary Search Tree Value 最近的二分搜索树的值
- [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
- leetcode 669. Trim a Binary Search Tree 修剪二叉搜索树 (简单)
- leetcode 114. Flatten Binary Tree to Linked List 二叉树展开为链表(简单)