【LeetCode】Add to List 606. Construct String from Binary Tree
2017-08-23 11:03
357 查看
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.
The null node needs to be represented by empty parenthesis pair “()”. And you need to omit all the empty parenthesis pairs that don’t affect the one-to-one mapping relationship between the string and the original binary tree.
Example 1:
Input: Binary tree: [1,2,3,4]
1
/ \
2 3
/
4
Output: "1(2(4))(3)"
Explanation: Originallay 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)".
Example 2:
Input: Binary tree: [1,2,3,null,4]
1
/ \
2 3
\
4
Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
题目是把二叉树转成带括号的字符串显示。
The null node needs to be represented by empty parenthesis pair “()”. And you need to omit all the empty parenthesis pairs that don’t affect the one-to-one mapping relationship between the string and the original binary tree.
Example 1:
Input: Binary tree: [1,2,3,4]
1
/ \
2 3
/
4
Output: "1(2(4))(3)"
Explanation: Originallay 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)".
Example 2:
Input: Binary tree: [1,2,3,null,4]
1
/ \
2 3
\
4
Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
题目是把二叉树转成带括号的字符串显示。
class Solution { public: string ans; void DFS(TreeNode* t){ if(t==NULL)return; ans+=to_string(t->val); if(t->left||t->right){ //如果左子树为空,右子树不为空,左边的括号还是需要打印的 ans+='('; DFS(t->left); ans+=')'; } if(t->right){ ans+='('; DFS(t->right); ans+=')'; } } string tree2str(TreeNode* t) { DFS(t); return ans; } };
相关文章推荐
- LeetCode- Add to List 88. Merge Sorted Array
- leetcode_ Add to List 121. Best Time to Buy and Sell Stock
- 【LeetCode】Add to List 637. Average of Levels in Binary Tree
- leetcode_ Add to List 108. Convert Sorted Array to Binary Search Tree
- Leetcode--Add to List 374. Guess Number Higher or Lower
- Leetcode--Add to List 215. Kth Largest Element in an Array
- LeetCode-Add to List 71. Simplify Path
- 【LeetCode24】. Swap Nodes in Pairs Add to List
- LeetCode - Add to List 557. Reverse Words in a String III
- leetcode- Add to List 458. Poor Pigs
- leetCode_Add to List 206. Reverse Linked List
- [leetcode] Add to List 403. Frog Jump
- leetcode- Add to List 669. Trim a Binary Search Tree
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
- LeetCode 143 Reorder List Add to List
- [leetcode] Add to List 169. Majority Element
- [Leetcode] 2.Add Two Numbers(List To Long,模拟)
- LeetCode-Add to List 494. Target Sum
- [leetcode] 64. Minimum Path Sum Add to List
- LeetCode- Add to List 84. Largest Rectangle in Histogram