diff options
Diffstat (limited to '606')
-rwxr-xr-x | 606/main.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/606/main.py b/606/main.py new file mode 100755 index 0000000..44a82c7 --- /dev/null +++ b/606/main.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import typing + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def tree2str(self, root: typing.Optional[TreeNode]) -> str: + if not root: + return "" + if not root.left and not root.right: + return str(root.val) + if not root.right: + return str(root.val) + "(" + self.tree2str(root.left) + ")" + return ( + str(root.val) + + "(" + + self.tree2str(root.left) + + ")(" + + self.tree2str(root.right) + + ")" + ) + + +def main(): + solution = Solution() + print(solution.tree2str()) + + +if __name__ == "__main__": + main() |