Binary Tree Validation
Start Timer
0:00:00
Binary Tree Validation
You are given the root of a binary tree. You need to 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 values less than or equal to the node’s value.
- The right subtree of a node contains only nodes with values greater than or equal to the node’s value.
- Both the left and right subtrees must also be binary search trees.
Given the function def is_valid_bst(root: Node) -> bool:
, return True if the binary tree is a valid BST. Otherwise, return False.
Example:
Input:
Converted Binary Tree.png
Output:
def is_valid_bst(Node(3)) -> True
Recommended questions for you
Personalized based on your user activity, skill level, and preferences.
.
.
.
.
.
.
.
.
.
Comments