Algo-Sim › Binary Trees & BST
Preorder Traversal (Iterative)
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Preorder with an explicit stack: pop a node, output it, push right then left so left comes off first.
Problem
Return a binary tree's preorder traversal (node, left, right) without recursion, using an explicit stack.
Constraints
- 0 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
- An explicit stack avoids stack overflow on very deep trees
Edge cases to test
- Empty tree:
[] - A left-only chain (stack depth n)
- One node
Follow-up questions
- Why push right before left?
- A stack pops the last push first, so pushing right first makes left come off next.
- Can you do it in O(1) space?
- Morris traversal threads each node’s inorder predecessor back to it and removes the thread afterwards: O(n) time, O(1) space.
Solution
Java
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
if (root != null) stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
out.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
return out;
}Python
def preorder_traversal(root):
out = []
stack = [root] if root else []
while stack:
node = stack.pop()
out.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return outC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> out = {};
deque<TreeNode *> stack = {};
if (root != nullptr)
stack.push_back(root);
while (!stack.empty()) {
TreeNode *node = stack.back();
stack.pop_back();
out.push_back(node->val);
if (node->right != nullptr)
stack.push_back(node->right);
if (node->left != nullptr)
stack.push_back(node->left);
}
return out;
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)