-
Notifications
You must be signed in to change notification settings - Fork 160
/
sum-of-root-to-leaf-binary-numbers.md
27 lines (18 loc) · 1.24 KB
/
sum-of-root-to-leaf-binary-numbers.md
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
<p>Given a binary tree, each node has value <code>0</code> or <code>1</code>. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is <code>0 -> 1 -> 1 -> 0 -> 1</code>, then this could represent <code>01101</code> in binary, which is <code>13</code>.</p>
<p>For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.</p>
<p>Return the sum of these numbers.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<p><span id="example-output-1"><img alt="" src="https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png" style="width: 304px; height: 200px;" /></span></p>
<pre>
<strong>Input: </strong><span id="example-input-1-1">[1,0,1,0,1,0,1]</span>
<strong>Output: </strong><span id="example-output-1">22</span>
<strong>Explanation: </strong>(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
</pre>
<p> </p>
<p><strong>Note:</strong></p>
<ol>
<li>The number of nodes in the tree is between <code>1</code> and <code>1000</code>.</li>
<li>node.val is <code>0</code> or <code>1</code>.</li>
<li>The answer will not exceed <code>2^31 - 1</code>.</li>
</ol>