Python Preorder/Inorder/Postorder Iterative and Recursive Summary
Preorder:\n144. Binary Tree Preorder Traversal\n> Time Complexity O(N)\n> Space Complexity O(1)\n\n## Recursively\ndef preorderTraversal1(self, root):\n    res 
Preorder:\n**144. Binary Tree Preorder Traversal**\n> Time Complexity O(N)\n> Space Complexity O(1)\n```\n## Recursively\ndef preorderTraversal1(self, root):\n    res = []\n    self.dfs(root, res)\n    return res\n    \ndef dfs(self, root, res):\n    if root:\n        res.append(root.val)\n        self.dfs(root.left, res)\n        self.dfs(root.right, res)\n\t\t\n## Iteratively\ndef preorderTraversal(self, root):\n    def preorderTraversal(self, root):\n        stack, res = [], []\n        while stack or root:\n            if root:\n                stack.append(root)\n                res.append(root.val)    \n                root = root.left     \n            else:\n                node = stack.pop()\n                root = node.right\n        return res\n```\nInorder\n**94. Binary Tree Inorder Traversal**\n> Time Complexity O(N)\n> Space Complexity O(1)\n```\n\n## Recursively\ndef inorderTraversal(self, root):\n\tres = []\n\tself.helper(root, res)\n\treturn res\n\ndef helper(self, root, res):\n\tif root:\n\t\tself.helper(root.left, res)\n\t\tres.append(root.val)\n\t\tself.helper(root.right, res)\n\n## Iteratively\ndef inorderTraversal(self, root):\n    res = []\n    stack = []\n    while stack or root:\n        if root:\n            stack.append(root)\n            root = root.left\n        else:\n            node = stack.pop()\n            res.append(node.val)\n            root = node.right   \n    return res\n```\nPreorder\n**145. Binary Tree Postorder Traversal**\n> Time Complexity O(N)\n> Space Complexity O(1)\n```\n## Recursively\ndef postorderTraversal(self, root):\n\tself.res = []\n\tself.dfs(root)\n\treturn self.res\n\ndef dfs(self, root):\n\tif not root:\n\t\treturn \n\tself.dfs(root.left)\n\tself.dfs(root.right) \n\tself.res.append(root.val)\n\t\n## Iteratively\ndef postorderTraversal(self, root):\n\tstack, res = [], []\n\twhile stack or root:\n\t\tif root:\n\t\t\tstack.append(root)\n\t\t\tres.append(root.val)    \n\t\t\troot = root.right \n\t\telse:\n\t\t\tnode = stack.pop()\n\t\t\troot = node.left\n\treturn res[::-1]\n```
['Tree']