1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/从根到叶的二进制数之和 [sum-of-root-to-leaf-binary-numbers].html

36 lines
1.2 KiB
HTML
Raw Normal View History

2022-03-27 20:46:41 +08:00
<p>给出一棵二叉树,其上每个结点的值都是&nbsp;<code>0</code>&nbsp;&nbsp;<code>1</code>&nbsp;。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。</p>
<ul>
<li>例如,如果路径为&nbsp;<code>0 -&gt; 1 -&gt; 1 -&gt; 0 -&gt; 1</code>,那么它表示二进制数&nbsp;<code>01101</code>,也就是&nbsp;<code>13</code>&nbsp;</li>
</ul>
<p>对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。</p>
<p>返回这些数字之和。题目数据保证答案是一个 <strong>32 位 </strong>整数。</p>
<p>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中的节点数在&nbsp;<code>[1, 1000]</code>&nbsp;范围内</li>
<li><code>Node.val</code>&nbsp;仅为 <code>0</code><code>1</code>&nbsp;</li>
</ul>