↓Skip to main content
  1. LeetCode/

LeetCode 1022: Sum of Root To Leaf Binary Numbers

·2 mins· ·
LeetCode Easy Tree Depth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-27

  • Submissions: Runtime Error → Wrong Answer ×2 → Runtime Error → Wrong Answer ×3 → Accepted → Runtime Error ×4 → Accepted → Wrong Answer ×2 → Accepted → Wrong Answer → Runtime Error → Wrong Answer → Runtime Error → Accepted

Approach
#

The same as LC 129 in binary: num = num * 2 + node.val (Tree Template 1), added to the total at each leaf.

My Attempts
#

This took 21 submissions in 2022, mostly from tracking paths as strings:

  • Settling paths at None positions: every leaf has two None children, so each path counted twice, and dividing the total by 2 broke whenever a node had one child. For [1, 1, 0, 0] the answer is 6 + 2 = 8; it returned 9.
  • int('', 2) on an empty string (ValueError), or adding strings to integers (TypeError).

Solution
#

class Solution:
    def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
        def dfs(node, num):
            if not node:
                return 0
            num = num * 2 + node.val
            if not node.left and not node.right:
                return num
            return dfs(node.left, num) + dfs(node.right, num)
        return dfs(root, 0)

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • Settle only at leaves; reaching None is not a path.

Interview Explanation
#

Recursing down, I shift the current number left and add the node’s bit, return it at a leaf, and sum both sides. Time O(n).

Related

LeetCode 938: Range Sum of BST
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 938 note: prune with the BST order, going only right below low and only left above high, adding nodes inside the range
LeetCode 965: Univalued Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 965 note: every value must equal the root’s; compare values while recursing, not the booleans the recursion returns
LeetCode 993: Cousins in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 993 note: cousins share a depth but not a parent; record x’s and y’s depth and parent during DFS and compare
LeetCode 872: Leaf-Similar Trees
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 872 note: collect leaves left to right with DFS; BFS collects level by level, which is not left-to-right order
LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 671: Second Minimum Node In a Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 671 note: the root is the minimum; the second minimum is the smallest value larger than the root, and only subtrees equal to the root need searching