|
| 1 | +# 144. Binary Tree Preorder Traversal - Best Practices of LeetCode Solutions |
| 2 | +LeetCode link: [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal), difficulty: **Easy**. |
| 3 | + |
| 4 | +## LeetCode description of "144. Binary Tree Preorder Traversal" |
| 5 | +Given the `root` of a binary tree, return _the preorder traversal of its nodes' values_. |
| 6 | + |
| 7 | +### [Example 1] |
| 8 | + |
| 9 | +**Input**: `root = [1,null,2,3]` |
| 10 | + |
| 11 | +**Output**: `[1,2,3]` |
| 12 | + |
| 13 | +**Explanation**: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +### [Example 2] |
| 18 | + |
| 19 | +**Input**: `root = [1,2,3,4,5,null,8,null,null,6,7,9]` |
| 20 | + |
| 21 | +**Output**: `[1,2,4,5,6,7,3,8,9]` |
| 22 | + |
| 23 | +**Explanation**: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +### [Example 3] |
| 28 | + |
| 29 | +**Input**: `root = []` |
| 30 | + |
| 31 | +**Output**: `[]` |
| 32 | + |
| 33 | +### [Example 4] |
| 34 | + |
| 35 | +**Input**: `root = [1]` |
| 36 | + |
| 37 | +**Output**: `[1]` |
| 38 | + |
| 39 | +### [Constraints] |
| 40 | +- The number of nodes in the tree is in the range `[0, 100]`. |
| 41 | +- `-100 <= Node.val <= 100` |
| 42 | + |
| 43 | +## Intuition |
| 44 | +Will be added later. |
| 45 | + |
| 46 | +## Steps |
| 47 | +Will be added later. |
| 48 | + |
| 49 | +## Complexity |
| 50 | +Recursive solution and iterative solution are equal in complexity. |
| 51 | + |
| 52 | +* Time: `O(N)`. |
| 53 | +* Space: `O(N)`. |
| 54 | + |
| 55 | +## Follow-up |
| 56 | +Recursive solution is trivial, could you do it iteratively? |
| 57 | + |
| 58 | +## Follow-up intuition |
| 59 | +Will be added later. |
| 60 | + |
| 61 | +## Python |
| 62 | +### Solution 1: Recursion |
| 63 | +```python |
| 64 | +class Solution: |
| 65 | + def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: |
| 66 | + self.values = [] |
| 67 | + |
| 68 | + self.traverse(root) |
| 69 | + |
| 70 | + return self.values |
| 71 | + |
| 72 | + def traverse(self, node): |
| 73 | + if node is None: |
| 74 | + return |
| 75 | + |
| 76 | + self.values.append(node.val) |
| 77 | + |
| 78 | + self.traverse(node.left) |
| 79 | + |
| 80 | + self.traverse(node.right) |
| 81 | +``` |
| 82 | + |
| 83 | +### Solution 2: Iteration |
| 84 | +```python |
| 85 | +class Solution: |
| 86 | + def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: |
| 87 | + values = [] |
| 88 | + stack = [] |
| 89 | + |
| 90 | + if root: |
| 91 | + stack.append(root) |
| 92 | + |
| 93 | + while stack: |
| 94 | + node = stack.pop() |
| 95 | + values.append(node.val) |
| 96 | + |
| 97 | + if node.right: |
| 98 | + stack.append(node.right) |
| 99 | + |
| 100 | + if node.left: |
| 101 | + stack.append(node.left) |
| 102 | + |
| 103 | + return values |
| 104 | +``` |
| 105 | + |
| 106 | +## JavaScript |
| 107 | +```javascript |
| 108 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 109 | +``` |
| 110 | + |
| 111 | +## Java |
| 112 | +```java |
| 113 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 114 | +``` |
| 115 | + |
| 116 | +## C++ |
| 117 | +```cpp |
| 118 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 119 | +``` |
| 120 | + |
| 121 | +## C# |
| 122 | +```c# |
| 123 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 124 | +``` |
| 125 | + |
| 126 | +## Go |
| 127 | +```go |
| 128 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 129 | +``` |
| 130 | + |
| 131 | +## Ruby |
| 132 | +```ruby |
| 133 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 134 | +``` |
| 135 | + |
| 136 | +## C, Kotlin, Swift, Rust or other languages |
| 137 | +``` |
| 138 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 139 | +``` |
0 commit comments