Post

Leetcode Day 14 - Binary Tree: Path and Construction Problems

Focuses on finding specific nodes or values in binary trees, such as the bottom-left value and path sums. Also covers reconstructing binary trees from inorder and postorder traversal.

Binary Tree

Link to note about binary tree

Find Bottom Left Tree Value

Link to Leetcode question1

Given the root of a binary tree, return the leftmost value in the last row of the tree.

Example 1

Desktop View

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

Example 2

Desktop View

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

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
22
23
24
25
26
27
28
class Solution(object):
    def findBottomLeftValue(self, root):
                
        def find(node):
            if node is None:
                return None, 0
            
            if node.left is None and node.right is None:
                return node.val, 1
            
            if node.left is not None:
                left, left_depth = find(node.left)
                left_depth += 1
            
                if node.right is None:
                    return left, left_depth
                else:
                    right, right_depth = find(node.right)
                    right_depth += 1
                    if right_depth > left_depth:
                        return right, right_depth
                    else:
                        return left, left_depth
            right, right_depth = find(node.right)
            return right, right_depth + 1
        
        re, temp = find(root)
        return re

Path Sum

Link to Leetcode question3

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

A leaf is a node with no children.

Example 1

Desktop View

1
2
3
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.

Example 2

Desktop View

1
2
3
4
5
6
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.

Example 3

1
2
3
Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.

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
23
class Solution(object):
    def hasPathSum(self, root, targetSum):

        def find(node,s):
            if node is None:
                return False
            
            if node.left is None and node.right is None and node.val == targetSum - s:
                return True
            
            s += node.val

            if node.left is not None:
                if find(node.left, s):
                    return True
            
            if node.right is not None:
                if find(node.right,s):
                    return True
            
            return False
    
        return find(root,0)

Similar Questions

Construct Binary Tree from Inorder and Postorder Traversal

Link to Leetcode question8

Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.

Example 1

Desktop View

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

Example 2

1
2
Input: inorder = [-1], postorder = [-1]
Output: [-1]

Solution

A detailed explaination of solution can be found here9.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution(object):
    def buildTree(self, inorder, postorder):

        def construct(inorder, postorder):
            if not inorder:
                return None

            mid = postorder.pop()
            i = inorder.index(mid)
            
            right_node = construct(inorder[i + 1:], postorder)
            left_node = construct(inorder[:i], postorder)

            return TreeNode(mid, left_node, right_node)
        
        return construct(inorder, postorder)

Similar Questions

Reference

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