1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/求根节点到叶节点数字之和 [sum-root-to-leaf-numbers].html
2022-03-29 12:43:11 +08:00

49 lines
1.7 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

给你一个二叉树的根节点 <code>root</code> ,树中每个节点都存放有一个 <code>0</code><code>9</code> 之间的数字。
<div class="original__bRMd">
<div>
<p>每条从根节点到叶节点的路径都代表一个数字:</p>
<ul>
<li>例如,从根节点到叶节点的路径 <code>1 -> 2 -> 3</code> 表示数字 <code>123</code></li>
</ul>
<p>计算从根节点到叶节点生成的 <strong>所有数字之和</strong></p>
<p><strong>叶节点</strong> 是指没有子节点的节点。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg" style="width: 212px; height: 182px;" />
<pre>
<strong>输入:</strong>root = [1,2,3]
<strong>输出:</strong>25
<strong>解释:</strong>
从根到叶子节点路径 <code>1->2</code> 代表数字 <code>12</code>
从根到叶子节点路径 <code>1->3</code> 代表数字 <code>13</code>
因此,数字总和 = 12 + 13 = <code>25</code></pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg" style="width: 292px; height: 302px;" />
<pre>
<strong>输入:</strong>root = [4,9,0,5,1]
<strong>输出:</strong>1026
<strong>解释:</strong>
从根到叶子节点路径 <code>4->9->5</code> 代表数字 495
从根到叶子节点路径 <code>4->9->1</code> 代表数字 491
从根到叶子节点路径 <code>4->0</code> 代表数字 40
因此,数字总和 = 495 + 491 + 40 = <code>1026</code>
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点的数目在范围 <code>[1, 1000]</code></li>
<li><code>0 <= Node.val <= 9</code></li>
<li>树的深度不超过 <code>10</code></li>
</ul>
</div>
</div>