Pre-order Traversal
It is named so, because the parent is considered before left child and right child. Let's say, a tree with node 3 has children node 1 and node 2. Then, in this traversal, we will print node 3 first, then node 1 and node 2 follows.
E.g. [3, 1, 2]
class Traversal:
def preOrderTraversal(self, root):
def fun(root, result):
if not root:
return
result.append(root.val)
fun(root.left, result)
fun(root.right, result)
result = []
fun(root, result)
return result
Now, go ahead and solve this problem: https://leetcode.com/problems/binary-tree-preorder-traversal
2
.jpg)