## Java binary tree post-order traversal (recursive/non-recursive)

Introduction: Traversal is one of the most basic operations on the tree. The so-called traversal of the binary tree is to walk through all the nodes of the binary tree according to certain rules and order, so that each node is visited once and only once.

Let L, D, and R represent traversing the left subtree, visiting the root node, and traversing the right subtree respectively, then there are several situations for traversing a binary tree: DLR (called pre-order traversal), LDR (called in-order traversal) traversal), LRD (called post-order traversal), level traversal.

## post order traversal

Post-order traversal: first traverse the left subtree, then traverse the right subtree, and finally visit the root node.

As shown in the figure, the post-order traversal result of the binary tree is: [8,9,4,15,7,20,3]

## Code

### recursive way

public List<Integer> postorderTraversal(TreeNode root) {

List<Integer> res = new ArrayList<Integer>();
postorder(root, res);
return res;
}

public void postorder(TreeNode root, List<Integer> res) {

if (root == null) {

return;
}
postorder(root.left, res);
postorder(root.right, res);
}

### non-recursive way

public List<Integer> postorderTraversal(TreeNode root) {

List<Integer> res = new ArrayList<Integer>();
if (root == null) {

return res;
}

TreeNode prev = null;
while (root != null || !stack.isEmpty()) {

while (root != null) {

stack.push(root);
root = root.left;
}
root = stack.pop();
if (root.right == null || root.right == prev) {

prev = root;
root = null;
} else {

stack.push(root);
root = root.right;
}
}
return res;
}

Full code:

public static void main(String[] args) {

TreeNode root = new TreeNode(3);
TreeNode n1 = new TreeNode(9);
TreeNode n2 = new TreeNode(20);
TreeNode n3 = new TreeNode(8);
TreeNode n4 = new TreeNode(15);
TreeNode n5 = new TreeNode(7);
TreeNode n6 = new TreeNode(4);
root.left = n1;
root.right = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n4.left = n6;

List<Integer> rs = postorderTraversal(root);
System.out.println("递归中序遍历结果：" + rs);

rs = postorderTraversal02(root);
System.out.println("非递归中序遍历结果：" + rs);
}

// 递归
public List<Integer> postorderTraversal(TreeNode root) {

List<Integer> res = new ArrayList<Integer>();
postorder(root, res);
return res;
}

public void postorder(TreeNode root, List<Integer> res) {

if (root == null) {

return;
}
postorder(root.left, res);
postorder(root.right, res);
}

// 非递归
public List<Integer> postorderTraversal02(TreeNode root) {

List<Integer> res = new ArrayList<Integer>();
if (root == null) {

return res;
}

TreeNode prev = null;
while (root != null || !stack.isEmpty()) {

while (root != null) {

stack.push(root);
root = root.left;
}
root = stack.pop();
if (root.right == null || root.right == prev) {

prev = root;
root = null;
} else {

stack.push(root);
root = root.right;
}
}
return res;
}

operation result:

### Guess you like

Origin blog.csdn.net/tracydragonlxy/article/details/106374449
Recommended
Ranking
Daily