mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
36 lines
1.2 KiB
HTML
36 lines
1.2 KiB
HTML
|
<p>给出一棵二叉树,其上每个结点的值都是 <code>0</code> 或 <code>1</code> 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>例如,如果路径为 <code>0 -> 1 -> 1 -> 0 -> 1</code>,那么它表示二进制数 <code>01101</code>,也就是 <code>13</code> 。</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。</p>
|
|||
|
|
|||
|
<p>返回这些数字之和。题目数据保证答案是一个 <strong>32 位 </strong>整数。</p>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>示例 1:</strong></p>
|
|||
|
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png" />
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>root = [1,0,1,0,1,0,1]
|
|||
|
<strong>输出:</strong>22
|
|||
|
<strong>解释:</strong>(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 2:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>root = [0]
|
|||
|
<strong>输出:</strong>0
|
|||
|
</pre>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>提示:</strong></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>树中的节点数在 <code>[1, 1000]</code> 范围内</li>
|
|||
|
<li><code>Node.val</code> 仅为 <code>0</code> 或 <code>1</code> </li>
|
|||
|
</ul>
|