1022. Sum of Root To Leaf Binary Numbers LeetCode Solution

In this guide, you will get 1022. Sum of Root To Leaf Binary Numbers LeetCode Solution with the best time and space complexity. The solution to Sum of Root To Leaf Binary Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sum of Root To Leaf Binary Numbers solution in C++
  4. Sum of Root To Leaf Binary Numbers solution in Java
  5. Sum of Root To Leaf Binary Numbers solution in Python
  6. Additional Resources
1022. Sum of Root To Leaf Binary Numbers LeetCode Solution image

Problem Statement of Sum of Root To Leaf Binary Numbers

You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.

For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.

For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
The test cases are generated so that the answer fits in a 32-bits integer.

Example 1:

Input: root = [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22

Example 2:

Input: root = [0]
Output: 0

Constraints:

The number of nodes in the tree is in the range [1, 1000].
Node.val is 0 or 1.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)
See also  470. Implement Rand10() Using Rand7() LeetCode Solution

1022. Sum of Root To Leaf Binary Numbers LeetCode Solution in C++

class Solution {
 public:
  int sumRootToLeaf(TreeNode* root) {
    int ans = 0;
    dfs(root, 0, ans);
    return ans;
  }

 private:
  void dfs(TreeNode* root, int val, int& ans) {
    if (root == nullptr)
      return;
    val = val * 2 + root->val;
    if (root->left == nullptr && root->right == nullptr)
      ans += val;
    dfs(root->left, val, ans);
    dfs(root->right, val, ans);
  }
};
/* code provided by PROGIEZ */

1022. Sum of Root To Leaf Binary Numbers LeetCode Solution in Java

class Solution {
  public int sumRootToLeaf(TreeNode root) {
    dfs(root, 0);
    return ans;
  }

  private int ans = 0;

  private void dfs(TreeNode root, int val) {
    if (root == null)
      return;
    val = val * 2 + root.val;
    if (root.left == null && root.right == null)
      ans += val;
    dfs(root.left, val);
    dfs(root.right, val);
  }
}
// code provided by PROGIEZ

1022. Sum of Root To Leaf Binary Numbers LeetCode Solution in Python

class Solution:
  def sumRootToLeaf(self, root: TreeNode | None) -> int:
    ans = 0

    def dfs(root: TreeNode | None, val: int) -> None:
      nonlocal ans
      if not root:
        return
      val = val * 2 + root.val
      if not root.left and not root.right:
        ans += val
      dfs(root.left, val)
      dfs(root.right, val)

    dfs(root, 0)
    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.