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)/根据二叉树创建字符串 [construct-string-from-binary-tree].html

34 lines
1.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

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.

<p>给你二叉树的根节点 <code>root</code> ,请你采用前序遍历的方式,将二叉树转化为一个由括号和整数组成的字符串,返回构造出的字符串。</p>
<p>空节点使用一对空括号对 <code>"()"</code> 表示,转化后需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。</p>
<div class="original__bRMd">
<div>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/cons1-tree.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>输入:</strong>root = [1,2,3,4]
<strong>输出:</strong>"1(2(4))(3)"
<strong>解释:</strong>初步转化后得到 "1(2(4)())(3()())" ,但省略所有不必要的空括号对后,字符串应该是"1(2(4))(3)" 。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/cons2-tree.jpg" style="width: 207px; height: 293px;" />
<pre>
<strong>输入:</strong>root = [1,2,3,null,4]
<strong>输出:</strong>"1(2()(4))(3)"
<strong>解释:</strong>和第一个示例类似,但是无法省略第一个空括号对,否则会破坏输入与输出一一映射的关系。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点的数目范围是 <code>[1, 10<sup>4</sup>]</code></li>
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
</ul>
</div>
</div>