Interview Query

Binary Tree Validation

Start Timer

0:00:00

Upvote
1
Downvote
Save question
Mark as completed
View comments (4)
Next question

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:

imageConverted Binary Tree.png

Output:

def is_valid_bst(Node(3)) -> True
.
.
.
.
.


Comments

Loading comments