Bird
Raised Fist0
DSA Rustprogramming~5 mins

Validate if Tree is BST in DSA Rust - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does BST stand for and what is its key property?
BST stands for Binary Search Tree. Its key property is that for every node, all values in the left subtree are smaller, and all values in the right subtree are larger than the node's value.
Click to reveal answer
beginner
What is the main idea behind validating if a binary tree is a BST?
The main idea is to check if every node's value lies within a valid range, which is updated as we traverse the tree, ensuring left children are less and right children are greater than their parent nodes.
Click to reveal answer
intermediate
In Rust, which data structure is commonly used to represent a binary tree node?
In Rust, a binary tree node is often represented using an Option, where TreeNode is a struct containing value and optional left and right children.
Click to reveal answer
intermediate
Why do we use Option and Box in Rust when implementing trees?
Option is used to represent the presence or absence of a child node (None means no child). Box is used to allocate nodes on the heap because tree nodes have recursive types and need a fixed size pointer.
Click to reveal answer
beginner
What is the time complexity of validating a BST by checking all nodes once?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the validation.
Click to reveal answer
What condition must hold true for every node in a BST?
ANo condition is required
BLeft subtree values > node > Right subtree values
CAll subtree values are equal to node
DLeft subtree values < node < Right subtree values
Which Rust type is used to represent a node that might be missing (no child)?
ABox<TreeNode>
BOption<Box<TreeNode>>
CVec<TreeNode>
DTreeNode
What is the best way to check if a binary tree is a BST?
ACheck if left child < node and right child > node only
BCheck if all nodes are unique
CUse a range to validate each node's value recursively
DCount nodes and compare with expected number
What is the time complexity of validating a BST by visiting each node once?
AO(n)
BO(1)
CO(log n)
DO(n^2)
Why do we use Box in Rust for tree nodes?
ATo allow recursive types by allocating on the heap
BTo store nodes on the stack
CTo make nodes immutable
DTo avoid using Option
Explain how to validate if a binary tree is a BST using recursion and value ranges.
Think about how the allowed value range changes as you go down the tree.
You got /4 concepts.
    Describe how Rust's Option and Box types help in building a binary tree structure.
    Consider why recursive types need pointers and how Rust handles optional values.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does it mean for a binary tree to be a Binary Search Tree (BST)?
      easy
      A. Every node's left subtree contains only nodes with values less than the node's value, and the right subtree only nodes with values greater.
      B. Every node has exactly two children.
      C. All nodes have the same value.
      D. The tree is balanced with equal height on both sides.

      Solution

      1. Step 1: Understand BST property

        A BST requires that for each node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater.
      2. Step 2: Compare options with definition

        Every node's left subtree contains only nodes with values less than the node's value, and the right subtree only nodes with values greater. correctly states this property, while others describe different tree properties.
      3. Final Answer:

        Every node's left subtree contains only nodes with values less than the node's value, and the right subtree only nodes with values greater. -> Option A
      4. Quick Check:

        BST property = Every node's left subtree contains only nodes with values less than the node's value, and the right subtree only nodes with values greater. [OK]
      Hint: BST means left < node < right for all nodes [OK]
      Common Mistakes:
      • Confusing BST with balanced tree
      • Thinking all nodes must have two children
      • Assuming all values are equal
      2. Which Rust function signature correctly represents a function to check if a binary tree is a BST?
      easy
      A. fn is_bst(root: &TreeNode) -> i32
      B. fn is_bst(root: TreeNode) -> bool
      C. fn is_bst(root: &Option) -> bool
      D. fn is_bst(root: Option<TreeNode>) -> String

      Solution

      1. Step 1: Understand Rust tree node usage

        Binary trees in Rust are often represented as Option to allow for empty nodes and heap allocation.
      2. Step 2: Check function signature return type

        The function should return a boolean indicating if the tree is a BST, so return type bool is correct.
      3. Final Answer:

        fn is_bst(root: &Option) -> bool -> Option C
      4. Quick Check:

        Correct Rust signature = fn is_bst(root: &Option) -> bool [OK]
      Hint: Use &Option and bool return for BST check [OK]
      Common Mistakes:
      • Using TreeNode directly without Option or Box
      • Returning wrong type like i32 or String
      • Not using reference for input parameter
      3. Given the following Rust code snippet, what will be the output?
      struct TreeNode {
          val: i32,
          left: Option,
          right: Option,
      }
      
      fn is_bst(node: &Option, min: Option<i32>, max: Option<i32>) -> bool {
          match node {
              None => true,
              Some(n) => {
                  if let Some(min_val) = min {
                      if n.val <= min_val { return false; }
                  }
                  if let Some(max_val) = max {
                      if n.val >= max_val { return false; }
                  }
                  is_bst(&n.left, min, Some(n.val)) && is_bst(&n.right, Some(n.val), max)
              }
          }
      }
      
      fn main() {
          let root = Some(Box::new(TreeNode {
              val: 10,
              left: Some(Box::new(TreeNode { val: 5, left: None, right: None })),
              right: Some(Box::new(TreeNode { val: 15, left: None, right: None })),
          }));
          println!("{}", is_bst(&root, None, None));
      }
      medium
      A. true
      B. false
      C. Compilation error
      D. Runtime panic

      Solution

      1. Step 1: Analyze tree structure

        The root node is 10, left child 5, right child 15. This satisfies BST property: 5 < 10 < 15.
      2. Step 2: Trace is_bst function calls

        Function checks node values against min and max bounds recursively. All nodes satisfy constraints, so returns true.
      3. Final Answer:

        true -> Option A
      4. Quick Check:

        Tree is BST = true [OK]
      Hint: Check node values within min/max bounds recursively [OK]
      Common Mistakes:
      • Ignoring min/max bounds in recursion
      • Confusing <= and < in comparisons
      • Assuming tree is not BST without checking children
      4. Identify the bug in this Rust function that checks if a tree is a BST:
      fn is_bst(node: &Option, min: Option<i32>, max: Option<i32>) -> bool {
          match node {
              None => true,
              Some(n) => {
                  if let Some(min_val) = min {
                      if n.val < min_val { return false; }
                  }
                  if let Some(max_val) = max {
                      if n.val > max_val { return false; }
                  }
                  is_bst(&n.left, min, Some(n.val)) && is_bst(&n.right, Some(n.val), max)
              }
          }
      }
      medium
      A. The function does not handle empty trees
      B. The comparisons should use <= and >= instead of < and >
      C. The recursion calls have wrong parameters order
      D. The function should return i32 instead of bool

      Solution

      1. Step 1: Review comparison operators in BST validation

        BST requires node values to be strictly greater than min and strictly less than max, so using < and > is incorrect for duplicates.
      2. Step 2: Correct comparison operators

        Comparisons should be <= min_val and >= max_val to reject equal values and maintain BST property.
      3. Final Answer:

        The comparisons should use <= and >= instead of < and > -> Option B
      4. Quick Check:

        Use <= and >= for BST checks [OK]
      Hint: Use <= and >= to reject equal values in BST check [OK]
      Common Mistakes:
      • Using < and > allows duplicates breaking BST
      • Mixing up parameter order in recursion
      • Ignoring None case for empty trees
      5. You have a binary tree where some nodes have duplicate values equal to their parent. You want to validate if it is a BST allowing duplicates only on the right subtree. How should you modify the Rust BST validation function?
      hard
      A. Disallow all duplicates by using strict < and > comparisons
      B. Allow equal values only on the right by using n.val <= min_val for right subtree and n.val < max_val for left subtree
      C. Allow equal values only when moving to the right subtree by changing comparison to n.val < min_val and n.val > max_val
      D. Allow equal values only on the right by using n.val >= min_val for right subtree and n.val < max_val for left subtree

      Solution

      1. Step 1: Understand BST with duplicates on right

        Duplicates allowed only on right means left subtree nodes must be strictly less than current node, right subtree nodes can be equal or greater.
      2. Step 2: Adjust min and max comparisons accordingly

        For left subtree, use n.val < max_val (strict less), for right subtree use n.val >= min_val (allow equal or greater).
      3. Final Answer:

        Allow equal values only on the right by using n.val >= min_val for right subtree and n.val < max_val for left subtree -> Option D
      4. Quick Check:

        Duplicates right subtree = n.val >= min_val [OK]
      Hint: Allow equals only on right: use >= min_val, < max_val [OK]
      Common Mistakes:
      • Allowing duplicates on left subtree
      • Using <= for left subtree allowing duplicates
      • Not adjusting comparisons for duplicates
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard