> For the complete documentation index, see [llms.txt](https://ugopireddy.gitbook.io/leet-code-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ugopireddy.gitbook.io/leet-code-solutions/solutions_101_-_150/144_binary_tree_preorder_traversal__medium.md).

# 144 Binary Tree Preorder Traversal – Medium

### Problem:

Given a binary tree, return the preorder traversal of its nodes’ values.

For example: Given binary tree {1,#,2,3},

1\
2 / 3 return \[1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

### Thoughts:

It is trivial to use recursion approach to solve this problem.

Preorder is to do a root, left, right.

Always append when meets a root. Then go deeper to the left subtree if got one.

Using a Stack is very helpful, which guaranteed all elements in the subtree will be appended prior to the right child.

### Solutions:

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result= new LinkedList<Integer>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        if (root == null) {
            return result;
        }
        s.push(root);
        while (s.size() > 0) {
            TreeNode node = s.pop();
            result.add(node.val);
            if (node.right != null) {
                s.push(node.right);
            }
            if (node.left != null) {
                s.push(node.left);
            }
        }
        return result;
    }
   
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ugopireddy.gitbook.io/leet-code-solutions/solutions_101_-_150/144_binary_tree_preorder_traversal__medium.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
