|
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -<p>Given the <code>root</code> of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.</p> |
| 7 | +<p>Given the <code>root</code> node of a binary tree, your task is to create a string representation of the tree following a specific set of formatting rules. The representation should be based on a preorder traversal of the binary tree and must adhere to the following guidelines:</p> |
8 | 8 |
|
9 |
| -<p>Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.</p> |
| 9 | +<ul> |
| 10 | + <li> |
| 11 | + <p><strong>Node Representation</strong>: Each node in the tree should be represented by its integer value.</p> |
| 12 | + </li> |
| 13 | + <li> |
| 14 | + <p><strong>Parentheses for Children</strong>: If a node has at least one child (either left or right), its children should be represented inside parentheses. Specifically:</p> |
| 15 | + |
| 16 | + <ul> |
| 17 | + <li>If a node has a left child, the value of the left child should be enclosed in parentheses immediately following the node's value.</li> |
| 18 | + <li>If a node has a right child, the value of the right child should also be enclosed in parentheses. The parentheses for the right child should follow those of the left child.</li> |
| 19 | + </ul> |
| 20 | + </li> |
| 21 | + <li> |
| 22 | + <p><strong>Omitting Empty Parentheses</strong>: Any empty parentheses pairs (i.e., <code>()</code>) should be omitted from the final string representation of the tree, with one specific exception: when a node has a right child but no left child. In such cases, you must include an empty pair of parentheses to indicate the absence of the left child. This ensures that the one-to-one mapping between the string representation and the original binary tree structure is maintained.</p> |
| 23 | + |
| 24 | + <p>In summary, empty parentheses pairs should be omitted when a node has only a left child or no children. However, when a node has a right child but no left child, an empty pair of parentheses must precede the representation of the right child to reflect the tree's structure accurately.</p> |
| 25 | + </li> |
| 26 | + |
| 27 | +</ul> |
10 | 28 |
|
11 | 29 | <p> </p>
|
12 | 30 | <p><strong class="example">Example 1:</strong></p>
|
13 |
| -<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0606.Construct%20String%20from%20Binary%20Tree/images/cons1-tree.jpg" style="width: 292px; height: 301px;" /> |
| 31 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0606.Construct%20String%20from%20Binary%20Tree/images/cons1-tree.jpg" style="padding: 10px; background: #fff; border-radius: .5rem;" /> |
14 | 32 | <pre>
|
15 | 33 | <strong>Input:</strong> root = [1,2,3,4]
|
16 | 34 | <strong>Output:</strong> "1(2(4))(3)"
|
17 |
| -<strong>Explanation:</strong> Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)" |
| 35 | +<strong>Explanation:</strong> Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the empty parenthesis pairs. And it will be "1(2(4))(3)". |
18 | 36 | </pre>
|
19 | 37 |
|
20 | 38 | <p><strong class="example">Example 2:</strong></p>
|
21 |
| -<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0606.Construct%20String%20from%20Binary%20Tree/images/cons2-tree.jpg" style="width: 207px; height: 293px;" /> |
| 39 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0606.Construct%20String%20from%20Binary%20Tree/images/cons2-tree.jpg" style="padding: 10px; background: #fff; border-radius: .5rem;" /> |
22 | 40 | <pre>
|
23 | 41 | <strong>Input:</strong> root = [1,2,3,null,4]
|
24 | 42 | <strong>Output:</strong> "1(2()(4))(3)"
|
25 |
| -<strong>Explanation:</strong> Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output. |
| 43 | +<strong>Explanation:</strong> Almost the same as the first example, except the <code>()</code> after <code>2</code> is necessary to indicate the absence of a left child for <code>2</code> and the presence of a right child. |
26 | 44 | </pre>
|
27 | 45 |
|
28 | 46 | <p> </p>
|
|
0 commit comments