diff options
author | terminaldweller <devi@terminaldweller.com> | 2023-12-08 20:17:44 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2023-12-08 20:17:44 +0000 |
commit | a2dc0dceb46805eaa5485dd303e96532c2d6517d (patch) | |
tree | 2f0dc64c7fae309cf7f6de7deb142a86622817c1 | |
parent | 1903 (diff) | |
download | leetcode-a2dc0dceb46805eaa5485dd303e96532c2d6517d.tar.gz leetcode-a2dc0dceb46805eaa5485dd303e96532c2d6517d.zip |
606
Diffstat (limited to '')
-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() |