We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cdf3526 commit 673a6f3Copy full SHA for 673a6f3
501-1000/606.py
@@ -0,0 +1,21 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode:
3
+# def __init__(self, val=0, left=None, right=None):
4
+# self.val = val
5
+# self.left = left
6
+# self.right = right
7
+class Solution:
8
+ def tree2str(self, root: Optional[TreeNode]) -> str:
9
+ if root is None:
10
+ return ''
11
+ s = str(root.val)
12
+ if root.left is None and root.right is None:
13
+ s += ''
14
+ if root.left:
15
+ s += '({})'.format(self.tree2str(root.left))
16
+ if root.left is None and root.right:
17
+ s += '()'
18
+ if root.right:
19
+ s+= '({})'.format(self.tree2str(root.right))
20
+ return s
21
+
0 commit comments