0
0
DSA Goprogramming~20 mins

Validate if Tree is BST in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BST Validation on a Simple Tree
What is the output of the following Go code that checks if a binary tree is a BST?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isBST(root *TreeNode, min, max *int) bool {
    if root == nil {
        return true
    }
    if (min != nil && root.Val <= *min) || (max != nil && root.Val >= *max) {
        return false
    }
    return isBST(root.Left, min, &root.Val) && isBST(root.Right, &root.Val, max)
}

func main() {
    root := &TreeNode{Val: 10}
    root.Left = &TreeNode{Val: 5}
    root.Right = &TreeNode{Val: 15}
    root.Right.Left = &TreeNode{Val: 6}
    root.Right.Right = &TreeNode{Val: 20}
    fmt.Println(isBST(root, nil, nil))
}
Afalse
Btrue
CCompilation error
Dpanic: runtime error
Attempts:
2 left
💡 Hint
Check if all nodes in the right subtree are greater than the root.
Predict Output
intermediate
2:00remaining
Output of BST Validation on a Perfect BST
What does the following Go code print when checking if the tree is a BST?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isBST(root *TreeNode, min, max *int) bool {
    if root == nil {
        return true
    }
    if (min != nil && root.Val <= *min) || (max != nil && root.Val >= *max) {
        return false
    }
    return isBST(root.Left, min, &root.Val) && isBST(root.Right, &root.Val, max)
}

func main() {
    root := &TreeNode{Val: 8}
    root.Left = &TreeNode{Val: 3}
    root.Right = &TreeNode{Val: 10}
    root.Left.Left = &TreeNode{Val: 1}
    root.Left.Right = &TreeNode{Val: 6}
    root.Right.Right = &TreeNode{Val: 14}
    fmt.Println(isBST(root, nil, nil))
}
Afalse
BCompilation error
Ctrue
Dpanic: runtime error
Attempts:
2 left
💡 Hint
All left children are less and right children are greater than their parent nodes.
🔧 Debug
advanced
2:00remaining
Identify the Bug in BST Validation Code
What error will the following Go code produce when run?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isBST(root *TreeNode, min, max int) bool {
    if root == nil {
        return true
    }
    if root.Val <= min || root.Val >= max {
        return false
    }
    return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max)
}

func main() {
    root := &TreeNode{Val: 5}
    root.Left = &TreeNode{Val: 1}
    root.Right = &TreeNode{Val: 7}
    fmt.Println(isBST(root, -2147483648, 2147483647))
}
ACompilation error: cannot use int for min/max pointers
Bfalse
Cpanic: nil pointer dereference
Dtrue
Attempts:
2 left
💡 Hint
Check how min and max are passed and used.
🧠 Conceptual
advanced
1:30remaining
Why Use Min and Max Pointers in BST Validation?
Why do some BST validation functions use pointers for min and max values instead of plain integers?
ATo improve performance by avoiding copying integers
BTo allow nil values representing no boundary constraints at the start
CBecause Go does not support passing integers by value
DTo enable concurrent access to min and max values
Attempts:
2 left
💡 Hint
Think about how to represent the absence of a limit.
🚀 Application
expert
2:30remaining
Count Nodes Violating BST Property
Given the following tree, how many nodes violate the BST property?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func countViolations(root *TreeNode, min, max *int) int {
    if root == nil {
        return 0
    }
    count := 0
    if (min != nil && root.Val <= *min) || (max != nil && root.Val >= *max) {
        count = 1
    }
    count += countViolations(root.Left, min, &root.Val)
    count += countViolations(root.Right, &root.Val, max)
    return count
}

func main() {
    root := &TreeNode{Val: 10}
    root.Left = &TreeNode{Val: 5}
    root.Right = &TreeNode{Val: 15}
    root.Left.Left = &TreeNode{Val: 1}
    root.Left.Right = &TreeNode{Val: 12}
    root.Right.Left = &TreeNode{Val: 6}
    root.Right.Right = &TreeNode{Val: 20}
    fmt.Println(countViolations(root, nil, nil))
}
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Check each node if it violates min/max constraints during traversal.