From a2dc0dceb46805eaa5485dd303e96532c2d6517d Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 8 Dec 2023 15:17:44 -0500 Subject: 606 --- 606/main.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 606/main.py 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() -- cgit v1.2.3