Validate Binary Search Tree (C# and Python3)

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

valid 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.

Preorder traversal

Preorder: A B D E C F (First time traversal)

Inorder traversal

Inorder: D B E A F C (Second time traversal)

Postorder traversal

Postorder: D E B F C A (Last time traversal)

Solution (Stack + Inorder traversal)

C#

Python3

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Open chat
Powered by