Files
cpp-flashcards/org/study_deck_02/dsa/trees/0098-validate-binary-search-tree.org
T

77 lines
1.8 KiB
Org Mode
Raw Normal View History

#+PROPERTY: STUDY_DECK_02
* TODO 0098. Validate Binary Search Tree :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0098. Validate Binary Search Tree][0098. Validate Binary Search Tree]]
:END:
Given the ~root~ of a binary tree, /determine if it is a valid binary search tree (BST)/.
A *valid BST* is defined as follows:
- The left subtree of a node contains only nodes with keys *strictly less than* the node's key.
- The right subtree of a node contains only nodes with keys *strictly greater than* the node's key.
- Both the left and right subtrees must also be binary search trees.
*Example 1:*
#+begin_src
Input: root = [2,1,3]
Output: true
#+end_src
*Example 2:*
#+begin_src
Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.
#+end_src
*Constraints:*
- The number of nodes in the tree is in the range ~[1, 10^{4}]~.
- ~-2^{31} <= Node.val <= 2^{31} - 1~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
/**
* 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) {
}
};
#+end_src