Binary Search Tree-BST



John Rankin @ May 10, 2023

Binary Search Tree (BST) is a data structure that is often used in coding interviews. It is a binary tree where all the nodes on the left side of a given node are smaller and all the nodes on the right side are larger. This makes it an efficient data structure for searching, inserting, and deleting elements.

Here are some common coding interview questions that can be solved using the Binary Search Tree pattern:

1. Validate Binary Search Tree

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

Example:

Input: [2,1,3]
Output: true
Solution:
class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def validate(node, low, high):
            if not node:
                return True
            if not low < node.val < high:
                return False
            return (validate(node.left, low, node.val) and
                    validate(node.right, node.val, high))

        return validate(root, float('-inf'), float('inf'))

2. Binary Tree Inorder Traversal

Given the root of a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
Output: [1,3,2]
Solution:
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        def dfs(node, res):
            if not node:
                return
            dfs(node.left, res)
            res.append(node.val)
            dfs(node.right, res)

        res = []
        dfs(root, res)
        return res

3. Binary Tree Maximum Path Sum

Given a non-empty binary tree, find the maximum path sum.

Example:

Input: [-10,9,20,null,null,15,7]
Output: 42
Explanation: The path with the maximum sum goes through nodes 15 -> 20 -> 7.

Solution:

class Solution:
    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        def dfs(node):
            if not node:
                return 0
            left_sum = max(dfs(node.left), 0)
            right_sum = max(dfs(node.right), 0)
            nonlocal max_sum
            max_sum = max(max_sum, left_sum + node.val + right_sum)
            return node.val + max(left_sum, right_sum)

        max_sum = float('-inf')
        dfs(root)
        return max_sum

These are just a few examples of how the Binary Search Tree pattern can be used to solve coding interview questions. By understanding this pattern and practicing similar problems, you can improve your problem-solving skills and increase your chances of success in coding interviews.


 

 

 

Most Recent Posts


Bitwise XOR-exclusive OR

Read This
May 10, 2023

0-1 Knapsack

Read This
May 10, 2023

K-Way Merge

Read This
May 10, 2023

Subsets

Read This
May 10, 2023

Backtracking

Read This
May 10, 2023

Greedy Algorithm

Read This
May 10, 2023

Trie

Read This
May 10, 2023

Depth-First Search-DFS

Read This
May 10, 2023

Breadth-First Search-BFS

Read This
May 10, 2023

Binary Search Tree-BST

Read This
May 10, 2023

Top K Elements

Read This
May 10, 2023

Binary Search Pattern

Read This
May 10, 2023

Cyclic Sort pattern

Read This
May 10, 2023

Merge Intervals

Read This
May 10, 2023

Day 7 - DynamoDB - and Working with 2 Services - Lambda

Read This
August 25, 2018

Day 6 - Lambda - Creating an API

Read This
August 23, 2018

AWS - Day 5 - S3 - Simple Storage Service

Read This
August 22, 2018

AWS - Day 4 - AWS CLI Useful Scripts

Read This
August 21, 2018

AWS - Day 3 - Create Container from another container

Read This
August 20, 2018

Day 2 - Docker Intro

Read This
August 17, 2018

AWS - Day1 - Tutorial - Launching my first Docker Container

Read This
August 16, 2018

AWS - Day 1 - Signing up and testing out their tutorials

Read This
August 16, 2018

Dynamic Programming - Edit Distance

Read This
December 19, 2016

Dynamic Programming - Fibonacci

Read This
December 19, 2016