98. Validate Binary Search Tree
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.
- A single node tree is a BST
Example
Example 1:
Input: {-1}
Output:true
Explanation:
For the following binary tree(only one node):
-1
This is a binary search tree.
Example 2:
Input: {2,1,4,#,#,3,5}
Output: true
For the following binary tree:
2
/ \
1 4
/ \
3 5
This is a binary search tree.
Example 3:
Input: {5,1,4,#,#,3,6}
Output: false
For the following binary tree:
5
/ \
1 4
/ \
3 6
Explanation: The root node's value is 5 but its right child's value is 4.
method: recursion
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- bool isValidBST(TreeNode* root) {
- return helper(root, NULL, NULL);
- }
- bool helper(TreeNode* root, TreeNode* lower, TreeNode* upper) {
- if (!root) return true;
- if (lower && lower->val >= root->val) return false;
- if (upper && upper->val <= root->val) return false;
- if (!helper(root->right, root, upper)) return false;
- if (!helper(root->left, lower, root)) return false;
- return true;
- }
- };
method: iteration
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- /**
- * @param root: The root of binary tree.
- * @return: True if the binary tree is BST, or false
- */
- bool isValidBST(TreeNode * root) {
- // write your code here
- if (!root) return true;
- if (!root->left && !root->right) return true;
- stack<TreeNode*> st;
- TreeNode* node = NULL;
- while (!st.empty() || root) {
- while (root) {
- st.push(root);
- root = root->left;
- }
- root = st.top();
- st.pop();
- if (node && root->val <= node->val) return false;
- node = root;
- root = root->right;
- }
- return true;
- }
- };

Plus, it's potential for players to further lower the home edge, 카지노 reaching 1.4%. This happens when the ball rests in the colored pocket with the zero-digit on it. This means that all of the bets that players have positioned in the round will proceed to be qualified in the next round.
ReplyDelete