Post

Leetcode Day 12 - Binary Tree: Tree Structure and Depth

Involves manipulating binary tree structures, such as inverting a binary tree and determining symmetry. Also covers finding the maximum and minimum depth of a tree.

Binary Tree

Link to note about binary tree

Invert Binary Tree

Link to Leetcode question1

Given the root of a binary tree, invert the tree, and return its root.

Desktop View

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

Example 2

Desktop View

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

Example 3

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

Solution

A detailed explaination of solution can be found here2.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
    def invertTree(self, root):
        if root is None:
            return
        
        temp = root.left
        root.left = root.right
        root.right = temp

        self.invertTree(root.left)
        self.invertTree(root.right)

        return root

Similar Questions

DiffSimilar QuestionsPythonJava
Medium2415 Reverse Odd Levels of Binary Tree3 

Symmetric Tree

Link to Leetcode question[^rnnfeol]

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1

Desktop View

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

Example 2

Desktop View

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

Solution

A detailed explaination of solution can be found here4.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        
        def compare(left, right):
            if left is None and right is None:
                return True
            elif left is None and right is not None:
                return False
            elif right is None and left is not None:
                return False
            elif left.val != right.val:
                return False
            
            outside = compare(left.left, right.right)
            inside = compare(left.right, right.left)
            return outside and inside
        
        return compare(root.left, root.right)

Maximum Depth of Binary Tree

Link to Leetcode question5

Example 1

Desktop View

1
2
Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2

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

Solution

A detailed explaination of solution can be found here6.

Python

1
2
3
4
5
6
7
8
9
class Solution(object):
    def maxDepth(self, root):
        def depth_of(node, depth):
            if node is None:
                return depth
            
            return 1 + max(depth_of(node.left, depth), depth_of(node.right, depth))
        
        return depth_of(root, 0)

Minimum Depth of Binary Tree

Link to Leetcode question7

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Example 1

Desktop View

1
2
Input: root = [3,9,20,null,null,15,7]
Output: 2

Example 2

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

Solution

A detailed explaination of solution can be found here6.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
    def minDepth(self, root):
        if root is None:
                return 0
        depth = 1
        if root.left is not None and root.right is not None:
            depth += min(self.minDepth(root.left), self.minDepth(root.right))
        elif root.left is not None:
            depth += self.minDepth(root.left)
        elif root.right is not None:
            depth += self.minDepth(root.right)

        return depth

Reference

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