[Leetcode] Binary tree-- 606. Construct String from Binary Tree

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


Solution: 
 1. use recursive way
  e.g.
         1
       /   \
      2     3
     /  \   
    4    5  
 
  2's subtree have the similar substructure, having the similar problem.  2(4)(5)     
  inside the whole answer 1(2(4)(5))(3)
  so it is easy to use recursive way to construct the string
 
1         if t is None:
2             return ""
3         strRes = str(t.val)
4         
5         if t.left or t.right:
6             strRes += "(" + self.tree2str(t.left) + ")"
7         if  t.right:
8             strRes += "(" + self.tree2str(t.right) + ")"
9         return strRes

 

  2.  use iterative way

    how to add right parentheses ")"?  one way is we can judge to visit the node again to complete the ")". Hence if it is visited before, then we add ")", also pop from the stack
    In one word, we use visited node set to record visited or not, and also stack to record traverse nodes as normal DFS
 
 1         stk = []           #stack
 2         visitedSet = set()
 3         strRes = ""
 4         if t is not None:
 5             #put t into stack
 6             stk.append(t)
 7         while (len(stk)):
 8             node = stk[-1]
 9             #print ("node: ", node.val)
10             if node not in visitedSet:
11                 visitedSet.add(node)
12                 #add "(" + val
13                 strRes += "(" + str(node.val)
14                 if node.right is not None:
15                     stk.append(node.right)
16                 if node.left is not None:
17                     stk.append(node.left)
18                 elif node.right is not None:
19                     strRes += "()"                 #make sure it doesn't affect the one-to-one mapping relationship
20                     
21             else:
22                 strRes += ")"
23                 #pop node
24                 stk.pop()
25         return strRes[1:-1]                   #remove the first and last parenthesis

 

 
    

转载于:https://www.cnblogs.com/anxin6699/p/7220468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值