Post

Leetcode Day 16 - Binary Tree: Binary Search Tree Special Cases

Concentrates on binary search tree (BST)-specific problems, such as finding the minimum absolute difference, locating the mode, and identifying the lowest common ancestor in both binary and binary search trees.

Binary Tree

Link to note about binary tree

Minimum Absolute Difference in BST

Link to Leetcode question1

Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.

Example 1

Desktop View

1
2
Input: root = [4,2,6,1,3]
Output: 1

Example 2

Desktop View

1
2
Input: root = [1,0,48,null,null,12,49]
Output: 1

Solution

A detailed explaination of solution can be found here2.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
    def getMinimumDifference(self, root):
        minab = [float('inf')]
        que = []
        
        def inorder(node):
            if node is None:
                return

            inorder(node.left)

            if len(que) > 0:
                prv = que[-1] 
                minab[0] = min(minab[0], abs(node.val - prv))

            que.append(node.val)

            inorder(node.right)
        
        inorder(root)
        return minab[0] 

Similar Questions

DiffSimilar QuestionsPythonJava
Medium532 K-diff Pairs in an Array3  

Find Mode in Binary Search Tree

Link to Leetcode question4

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1

Desktop View

1
2
Input: root = [1,null,2,2]
Output: [2]

Example 2

1
2
Input: root = [0]
Output: [0]

Solution

A detailed explaination of solution can be found here5.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
    def __init__(self):
        self.dict = {}
        self.ans=[]
        self.mx=-1
    def solve (self,root):
        if root is None: return
        self.dict[root.val] = self.dict.get(root.val, 0) + 1
        if self.dict[root.val]==self.mx:
            self.ans.append(root.val)
        elif self.dict[root.val]>self.mx:
            self.mx=self.dict[root.val]
            self.ans=[]
            self.ans.append(root.val)

        self.solve(root.left)
        self.solve(root.right)
    def findMode(self, root):
        
        self.solve(root)
        return self.ans

Lowest Common Ancestor of a Binary Tree

Link to Leetcode question6

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1

Desktop View

1
2
3
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2

Desktop View

1
2
3
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Example 3

1
2
Input: root = [1,2], p = 1, q = 2
Output: 1

Solution

A detailed explaination of solution can be found here7.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        if root is None:
            return None
        
        if root == p or root == q:
            return root

        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if left and right:
            return root
        
        return left if left else right

Similar Questions

Reference

This post is licensed under CC BY 4.0 by the author.