LeetCode 1022: Sum of Root To Leaf Binary Numbers Solution
Master LeetCode problem 1022 (Sum of Root To Leaf Binary Numbers), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1022. Sum of Root To Leaf Binary Numbers
Problem Explanation
Explanation
To solve this problem, we can perform a depth-first search (DFS) traversal of the binary tree while keeping track of the current binary number formed from the root to the current node. As we traverse the tree, we update the binary number accordingly. When we reach a leaf node, we add the binary number formed from the root to that leaf node to a running sum. Finally, we return the total sum of all the binary numbers formed from root to leaf nodes.
Algorithm:
- Initialize a global variable
totalSumto store the sum of all binary numbers formed from root to leaf nodes. - Implement a recursive DFS function that takes the current node and the binary number formed from the root to the current node as parameters.
- In the DFS function:
a. Update the binary number by left shifting it by 1 bit and adding the current node's value.
b. If the current node is a leaf node, add the binary number to the
totalSum. c. Recursively call the DFS function on the left child (if exists) with the updated binary number. d. Recursively call the DFS function on the right child (if exists) with the updated binary number. - Return
totalSumas the final result.
Time Complexity:
The time complexity of this approach is O(N), where N is the number of nodes in the binary tree. This is because we visit each node exactly once during the DFS traversal.
Space Complexity:
The space complexity of this approach is O(H), where H is the height of the binary tree. This space is used for the recursive call stack during the DFS traversal.
Solution Code
class Solution {
int totalSum = 0;
public int sumRootToLeaf(TreeNode root) {
dfs(root, 0);
return totalSum;
}
private void dfs(TreeNode node, int current) {
if (node == null) return;
current = (current << 1) | node.val;
if (node.left == null && node.right == null) {
totalSum += current;
return;
}
dfs(node.left, current);
dfs(node.right, current);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1022 (Sum of Root To Leaf Binary Numbers)?
This page provides optimized solutions for LeetCode problem 1022 (Sum of Root To Leaf Binary Numbers) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1022 (Sum of Root To Leaf Binary Numbers)?
The time complexity for LeetCode 1022 (Sum of Root To Leaf Binary Numbers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1022 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1022 in Java, C++, or Python.