Generally, tree traversal can be specified as inorder traversal (only for binary tree) , preorder traversal, postorder traversal and level order traversal. Level order traversal can be easily implemented with a queue. But other three traversals could be implemented recursively, iteratively with O(n) space and even iteratively with O(1) space.

We focus on binary tree first.

The easiest way of binary tree’s inorder traversal, preorder traversal, postorder traversal is the recursive way. In fact, it is the definition of them.

//Inorder
/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> inorderTraversal(TreeNode root) {
List&lt;Integer> res = new ArrayList&lt;>();
helper(root, res);
return res;
}

public void helper(TreeNode root, List&lt;Integer> res)
{
if(root != null)
{
helper(root.left, res);
helper(root.right, res);
}
}
}

//Preorder
/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
List&lt;Integer> res;
public List&lt;Integer> preorderTraversal(TreeNode root) {
res = new ArrayList&lt;>();
if(root == null) return res;
helper(root);
return res;
}
public void helper(TreeNode root)
{
if(root != null)
{
helper(root.left);
helper(root.right);
}
}
}

//Postorder
/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> postorderTraversal(TreeNode root) {
List&lt;Integer> res = new ArrayList&lt;>();
helper(root, res);
return res;
}

public void helper(TreeNode root, List&lt;Integer> res)
{
if(root != null)
{
helper(root.left, res);
helper(root.right, res);
}
}
}


Then I’d like to talk about how to implement them iteratively. Using the stack is a direct idea. We could always push the left child to the stack and when the node is popped, the node’s left subtree has been visited. And then the node’s right subtree is solved as the same way.

For the inorder traversal, we should visit the node after its left subtree. So the node should be visited after it is popped.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> inorderTraversal(TreeNode root) {
List&lt;Integer> res = new ArrayList&lt;>();
Deque&lt;TreeNode> s = new ArrayDeque&lt;>();
TreeNode p=root;
while(s.size() > 0 || p != null)
{
if(p != null)
{
s.push(p);
p = p.left;
}
else
{
p = s.pop();
p = p.right;
}
}
return res;
}
}


And for preorder traversal, we should visit the node before its left subtree. So the node should be visited before it is pushed.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> preorderTraversal(TreeNode root) {
Deque&lt;TreeNode> s = new ArrayDeque&lt;>();
List&lt;Integer> res=new ArrayList&lt;>();
while(s.size()>0||root!=null)
{
if(root==null)
{
root=s.pop();
root=root.right;
}
else
{
s.push(root);
root=root.left;
}
}
return res;
}
}


For the postorder tree, there are two ways of implementing it.

The first way is easy to understand. We can implement preorder traversal from right. The normal preorder traversal is that we should first visit the root then visit the left subtree and visit the right subtree finally. But now we should first visit the root then visit the right subtree and visit the left subtree finally. When we complete this traversal, we should reverse the result and output it. Then we get the postorder traversal result.

class Solution {
public List&lt;Integer> postorderTraversal(TreeNode root) {
Stack&lt;TreeNode> s = new Stack&lt;>();
while(s.size() > 0 || root != null)
{
if(root == null)
{
root = s.pop();
root = root.left;
}
else
{
s.push(root);
root = root.right;
}
}
return res;
}
}


However, there is a limit of this method. We have to complete the whole traversal to get the results. That means we could not make the postorder traversal an iterator.

So let’s focus on the second way. We can attach a boolean variable to each node to show if it is visited. In the postorder traversal we should visit the node after its left subtree and right subtree. So when we pop it at the first time, it should not be output as postorder traversal now. So if it is not visited, set it visited and push it back to the stack. Then visit its right subtree. When the right subtree is solved, the node should be popped again. Now its boolean variable shows it has been visited. We could output it.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
class NodeContainer{
TreeNode node;
boolean visited;
NodeContainer(TreeNode n, boolean flag)
{
node = n;
visited = flag;
}
}
public List&lt;Integer> postorderTraversal(TreeNode root) {
List&lt;Integer> res = new ArrayList&lt;>();
Deque&lt;NodeContainer> s = new ArrayDeque&lt;>();
while(s.size() > 0 || root != null)
{
if(root != null)
{
s.push(new NodeContainer(root, false));
root = root.left;
}
else
{
NodeContainer nc = s.pop();
root = nc.node;
if(!nc.visited)
{
nc.visited = true;
s.push(nc);
root = root.right;
}
else
{
root = null;// Set root to null in order to pop the stack in the next step
}
}
}
return res;
}
}


Now we will discuss the optimal method of the three binary tree traversal. It just need O(1) space. It is called Morris Traversal.

The difficulty of implementing it with O(1) space is that we have to return to the root when we visit its subtree. So we should find a way to record the root when visiting the subtree. In Morris Traversal, the idea is when we get a root, we should find the rightmost node in its left subtree. And set the rightmost node’s right child as the current root. Then when we completing visiting the left subtree, we could get the root back with the right child.

For inorder subtree, we should output the node after visiting its left subtree.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> inorderTraversal(TreeNode root) {
List&lt;Integer> res = new ArrayList&lt;>();
TreeNode cur = root, prev = null;
while(cur != null)
{
if(cur.left == null)
{
cur = cur.right;
}
else
{
prev = cur.left;
while(prev.right != null &amp;&amp; prev.right != cur)
{
prev = prev.right;
}
if(prev.right == null)
{
prev.right = cur;
cur = cur.left;
}
else
{
prev.right = null;
cur = cur.right;
}
}
}
return res;
}
}


For preorder traversal, we should output the node before visiting its left subtree.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> preorderTraversal(TreeNode root) {
List&lt;Integer> res = new ArrayList&lt;>();
TreeNode cur = root, prev = null;
while(cur != null)
{
if(cur.left == null)
{
cur = cur.right;
}
else
{
prev = cur.left;
while(prev.right != null &amp;&amp; prev.right != cur)
{
prev = prev.right;
}
if(prev.right == null)
{
prev.right = cur;
cur = cur.left;
}
else
{
prev.right = null;
cur = cur.right;
}
}
}
return res;
}
}


For postorder traversal, when we get back the node, we must reversely output the nodes in the path from the current node’s left child to its left subtree’s rightmost child. It guarantees the right subtree is visited before the root.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;Integer> postorderTraversal(TreeNode root) {
TreeNode dump = new TreeNode(0);
dump.left = root;
List&lt;Integer> res = new ArrayList&lt;>();
TreeNode cur = dump, prev = null;
while(cur != null)
{
if(cur.left == null)
{
cur = cur.right;
}
else
{
prev = cur.left;
while(prev.right != null &amp;&amp; prev.right != cur)
{
prev = prev.right;
}
if(prev.right == null)
{
prev.right = cur;
cur = cur.left;
}
else
{
printReverse(cur.left, prev, res);
prev.right = null;
cur = cur.right;
}
}
}
return res;
}

private void printReverse(TreeNode start, TreeNode end, List&lt;Integer> res)
{
List&lt;Integer> temp = new ArrayList&lt;>();
while(start != end.right)
{
start = start.right;
}
Collections.reverse(temp);
}
}


Although the O(1) space method is trivial, we can just remember the main idea, set the left subtree’s rightmost node’s right child to the current root. Then we could implement it based on this.

The binary tree’s level order traversal is very easy. We don’t discuss much in this article.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List&lt;List&lt;Integer>> levelOrder(TreeNode root) {
List&lt;List&lt;Integer>> res=new ArrayList&lt;>();
List&lt;Integer> resi;
q.offer(root);
TreeNode tmp;
int qs;
while(true)
{
qs = q.size();
resi = new ArrayList&lt;>();
for(int i = 0; i &lt; qs; i++)
{
tmp = q.poll();
if(tmp != null)
{
q.offer(tmp.left);
q.offer(tmp.right);
}
}
if(q.size() == 0) break;
}
return res;
}
}


We have discussed binary tree. Then for N-ary tree, it only has preorder traversal, postorder traversal and level order traversal. First, the recursive way of the preorder traversal and the postorder traversal is the following.

//Preorder
/*
// Definition for a Node.
class Node {
public int val;
public List&lt;Node> children;

public Node() {}

public Node(int _val,List&lt;Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List&lt;Integer> preorder(Node root) {
List&lt;Integer> res = new ArrayList&lt;>();
helper(root, res);
return res;
}
public void helper(Node root, List&lt;Integer> res)
{
if(root != null)
{
if(root.children != null)
{
for(int i = 0; i &lt; root.children.size(); i++)
{
helper(root.children.get(i), res);
}
}
}
}
}

//Postorder
/*
// Definition for a Node.
class Node {
public int val;
public List&lt;Node> children;

public Node() {}

public Node(int _val,List&lt;Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List&lt;Integer> postorder(Node root) {
List&lt;Integer> res = new ArrayList&lt;>();
helper(root, res);
return res;
}
public void helper(Node root, List&lt;Integer> res)
{
if(root != null)
{
if(root.children != null)
{
for(int i = 0; i &lt; root.children.size(); i++)
{
helper(root.children.get(i), res);
}
}
}
}
}


Then we can implement them iteratively with one stack.

For preorder traversal, for each root we could push its child from right to left. It can guarantee we can pop the node from the left child to the right.

//Iterative with one stack
/*
// Definition for a Node.
class Node {
public int val;
public List&lt;Node> children;

public Node() {}

public Node(int _val,List&lt;Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List&lt;Integer> preorder(Node root) {
List&lt;Integer> res = new ArrayList&lt;>();
if(root == null) return res;
Deque&lt;Node> s = new ArrayDeque&lt;>();
s.push(root);
while(s.size() > 0)
{
root = s.pop();
if(root.children != null)
{
for(int i = root.children.size() - 1; i >= 0; i--)
{
s.push(root.children.get(i));
}
}
}
return res;
}
}


For postorder traversal, we could use the reverse way.

/*
// Definition for a Node.
class Node {
public int val;
public List&lt;Node> children;

public Node() {}

public Node(int _val,List&lt;Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List&lt;Integer> postorder(Node root) {
Deque&lt;Node> s = new ArrayDeque&lt;>();
if(root == null) return res;
s.push(root);
while(s.size() > 0)
{
root = s.pop();
if(root.children != null)
{
for(int i = 0; i &lt; root.children.size(); i++)
{
s.push(root.children.get(i));
}
}
}
return res;
}
}


In this article, non-reverse postorder traversal and O(1) space traversal for N-ary tree will not be discussed because they are too complex.

And N-ary tree’s level order traversal is also easy.

/*
// Definition for a Node.
class Node {
public int val;
public List&lt;Node> children;

public Node() {}

public Node(int _val,List&lt;Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List&lt;List&lt;Integer>> levelOrder(Node root) {
List&lt;List&lt;Integer>> res = new ArrayList&lt;>();
if(root == null) return res;
int qsize;
Node tempnode;
List&lt;Integer> templist;
while(q.size() > 0)
{
qsize = q.size();
templist = new ArrayList&lt;>();
for(int i = 0; i &lt; qsize; i++)
{
tempnode = q.poll();
if(tempnode.children != null)
{
for(int j = 0; j &lt; tempnode.children.size(); j++)
{