Create viral videos with

GPT-4o + Ads library

Use GPT-4o to edit video empowered by Youtube & Tiktok & Facebook ads library. Turns your links or media assets into viral videos in one click.

Try it free

#Education

Before diving into the main content of this article, I would like to extend my gratitude to Relevel for sponsoring this entire video. Relevel, by Unacademy, is a hiring platform that requires no degree or prior experience. They offer mentoring sessions and job opportunities in front-end, back-end, and business development from top Indian companies, all for free. The entire hiring process is swift and simple—just give the relevant test, and based on your score, interviews will be scheduled and you'll get hired. Check out the links in the description and apply for the relevant tests as soon as possible.

In this lecture, we will explore the coding implementation of the pre-order traversal technique in binary trees. We have previously discussed DFS traversal methods, that include pre-order, in-order, and post-order traversals. Here, we focus specifically on pre-order traversal.

In preorder traversal, the sequence of visiting nodes is:

- Root
- Left
- Right

Let's implement a simple and intuitive approach to writing the preorder traversal code. The steps are straightforward:

- Visit the root node and print it.
- Recurse on the left subtree.
- Recurse on the right subtree.

Here is the C++/Java code for preorder traversal:

```
void preorder(Node* root) (
if(root == nullptr) return; // base case
cout << root->data << " "; // visit the root
preorder(root->left); // recur on the left subtree
preorder(root->right); // recur on the right subtree
)
```

Consider the following example tree for a step-by-step dry run:

```
1
/ \
2 3
/ \ \
4 5 7
/ \
6 8
\
9
\
10
```

Starting at the root node (1):

**Print 1**(root)- Move to the
**left subtree**and call preorder on node 2:**Print 2**(root)- Move to the left subtree and call preorder on node 4:
**Print 4**(root)- Both left and right of 4 are null, return to 2

- Move to the
**right subtree**and call preorder on node 5:**Print 5**(root)- Recur on left subtree, output is 6, right is null. So output sequence is 4, 2, 5, 6

- Move to the
**right subtree**and call preorder on node 3:**Print 3**(root)- Move to left, output is 7,
- Move to right and call preorder on node 8 (continuing in depth-first manner):
**Print 8**(root)- Move left, print 9, move right print 10.

- Combining, the final preorder traversal sequence is 1, 2, 4, 5, 6, 3, 7, 8, 9, 10.

**Time Complexity**: (O(n)), where (n) is the number of nodes in the tree, because each node is visited once.**Space Complexity**: The auxiliary space used in the recursive stack is (O(h)), where (h) is the height of the tree. In the worst case, this can be (O(n)) for a completely skewed tree.

- Preorder Traversal
- Binary Tree
- DFS (Depth-First Search)
- C++ Code
- Java Code
- Time Complexity
- Space Complexity
- Recursive Implementation

Preorder traversal in a binary tree is a depth-first search (DFS) technique where the nodes are recursively visited in this order: Root, Left subtree, Right subtree.

The time complexity of preorder traversal is (O(n)), where (n) is the number of nodes in the binary tree.

The space complexity of preorder traversal is (O(h)), where (h) is the height of the tree. In the worst-case scenario, the space complexity is (O(n)), especially for a completely skewed tree.

Yes, preorder traversal can be implemented iteratively using a stack to simulate the recursive call stack.

Preorder traversal is useful for creating a copy of the tree, for prefix expression evaluation, and for various tree-based algorithms where the root node needs to be processed before its child nodes.

In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.

TopView.ai provides two powerful tools to help you make ads video in one click.

Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.

Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.

You may also like