606. Construct String from Binary Tree
Simple DFS.
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:
1 | Input: Binary tree: [1,2,3,4] |
Example 2:
1 | Input: Binary tree: [1,2,3,null,4] |
Soulution:
1 | class Solution: |