Conquering Preorder Traversal: The Ultimate Java and Python Guide! ⚔️
🌲 "The Tree Kingdom and the Legend of Preorder Traversal!" ⚔️Once upon a time, in the mystical land of Binary Trees, a wandering coder (that’s YOU! 💪) embarked
# 🌲 "The Tree Kingdom and the Legend of Preorder Traversal!" ⚔️
 Once upon a time, in the mystical land of Binary Trees, a wandering coder (that’s YOU! 💪) embarked on a sacred quest… to conquer the kingdom of Preorder Traversal! 👑

The Tree Guardian appeared and whispered the Golden Rule of Preorder:
`🗡️ "Root ➝ Left ➝ Right"`

$$"Wait, that’s it?" you ask.$$
*YES, brave coder. That’s it. But beware! Many have fallen into the depths of confusion.*

#### Let’s decode this spell and solve the sacred riddle of traversal! 🧙‍♂️

## 🔥 The Ultimate Preorder Strategy (The Holy Scrolls)
1️⃣ Visit the Root First (Respect the king! 👑)
2️⃣ Go Left (Because lefties need love too.)
3️⃣ Go Right (Right-hand warriors stand strong!)

And thus, we set forth on our glorious coding adventure! 🚀

## ⚡ The 100% Beat Solutions!
🌀 Python (The Sorcerer's Way - Recursion) 🧙‍♂️
```
# "First me, then left, then right!" - The Tree
def preorderTraversal(root):
    return [root.val] + preorderTraversal(root.left) + preorderTraversal(root.right) if root else []

```
🔮 One-liner wizardry! No loops. No drama. Just pure recursion magic.

## ⚔️ Python (The Warrior's Way - Iterative) 🏹
```
# Stack-powered Kingdom Raid! ⚔️
def preorderTraversal(root):
    if not root: return []
    stack, result = [root], []
    
    while stack:
        node = stack.pop()
        result.append(node.val)
        if node.right: stack.append(node.right)  # Right child goes in last (so left is processed first)
        if node.left: stack.append(node.left)  # Left child goes in first
    return result

```
### 🔥 No recursion, just brute-force efficiency with a stack!

# ☕ Java Solutions (For the Code Sorcerers! 🔥)
### 🌀 Recursive Java (The Ancient Scrolls) 📜
```
import java.util.*;

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        preorder(root, result);
        return result;
    }

    private void preorder(TreeNode node, List<Integer> result) {
        if (node == null) return;
        result.add(node.val);
        preorder(node.left, result);
        preorder(node.right, result);
    }
}

```
📜 Ancient recursion magic at work!

## ⚔️ Iterative Java (The Algorithm Warrior) 🏹
```
import java.util.*;

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root != null) stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            result.add(node.val);
            if (node.right != null) stack.push(node.right);
            if (node.left != null) stack.push(node.left);
        }
        return result;
    }
}

```
# **UPVOTE**
![upvote.webp](https://assets.leetcode.com/users/images/ec1fd5d1-a81b-4359-b62f-ca1152e484fd_1743487740.9906132.webp)

['Stack', 'Tree', 'Depth-First Search', 'Recursion', 'Binary Tree', 'Java', 'Python3']