Summary of Binary Tree Traversal
statement
All the articles are my technical notes. Please indicate where they came from when they were reproduced.
[1] https://segmentfault.com/u/yzwall
[2] blog.csdn.net/j_dark/
Overview of 0-binary tree traversal
Binary tree traversal: according to the established order, each node is visited only once;
The idea of binary tree non-recursive traversal: Refer to this blog post The core idea is that there are overlapping elements in the local order to ensure the overall order. Because of the structural characteristics of the binary tree, each node in the binary tree (root node and leaf node) belongs to two local overlapping elements. For any overlapping element, it ensures that the local traversal is orderly and the whole traversal is orderly.
-
Where the coincident elements are located:
Locally all orderly, traverse the element and go out of the stack;
Local elements are not all ordered, and all unordered local elements are stacked. Because the stack is LIFO, local elements are stacked in reverse order.
TreeNode declaration of binary tree node
public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int val) { this.val = val; this.left = this.right = null; } }
1 Preorder traversal
1.1 Non-recursive Implementation
public class Solution { private class Pair { public TreeNode node; public boolean isVisited; public Pair(TreeNode node, boolean isVisited) { this.node = node; this.isVisited = isVisited; } } public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if (root == null) { return list; } ArrayDeque<Pair> stack = new ArrayDeque<Pair>(); stack.push(new Pair(root, false)); while (!stack.isEmpty()) { Pair top = stack.pop(); // The overlapping node completes all local ordering and pops up if (top.isVisited) { list.add(top.node.val); } else { // reverse: right -> left -> root if (top.node.right != null) { stack.push(new Pair(top.node.right, false)); } if (top.node.left != null) { stack.push(new Pair(top.node.left, false)); } stack.push(new Pair(top.node, true)); } } return list; } }
1.2 Recursive Implementation
public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if (root == null) { return list; } traverse(list, root); return list; } private void traverse(ArrayList<Integer>list, TreeNode root) { if (root == null) { return; } list.add(root.val); traverse(list, root.left); traverse(list, root.right); } }
2-order traversal
2.1 Non-recursive Implementation
public class Solution { private class Pair { public TreeNode node; public boolean isVisited; public Pair(TreeNode node, boolean isVisited) { this.node = node; this.isVisited = isVisited; } } public ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if (root == null) { return list; } ArrayDeque<Pair> stack = new ArrayDeque<Pair>(); stack.push(new Pair(root, false)); while (!stack.isEmpty()) { Pair top = stack.pop(); if (top.isVisited) { list.add(top.node.val); } else { // reverse: right -> root -> left if (top.node.right != null) { stack.push(new Pair(top.node.right, false)); } stack.push(new Pair(top.node, true)); if (top.node.left != null) { stack.push(new Pair(top.node.left, false)); } } } return list; } }
2.2 Recursive Implementation
public class Solution { public ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if (root == null) { return list; } traverse(list, root); return list; } private void traverse(ArrayList<Integer>list, TreeNode root) { if (root == null) { return; } traverse(list, root.left); list.add(root.val); traverse(list, root.right); } }
3 post-order traversal
3.1 Non-recursive Implementation
public class Solution { private class Pair { public TreeNode node; public boolean isVisited; public Pair(TreeNode node, boolean isVisited) { this.node = node; this.isVisited = isVisited; } } public ArrayList<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if (root == null) { return list; } ArrayDeque<Pair> stack = new ArrayDeque<Pair>(); stack.push(new Pair(root, false)); while (!stack.isEmpty()) { Pair top = stack.pop(); if (top.isVisited) { list.add(top.node.val); } else { // reverse: root -> right -> left stack.push(new Pair(top.node, true)); if (top.node.right != null) { stack.push(new Pair(top.node.right, false)); } if (top.node.left != null) { stack.push(new Pair(top.node.left, false)); } } } return list; } }
3.2 Recursive Implementation
public class Solution { public ArrayList<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if (root == null) { return list; } traverse(list, root); return list; } private void traverse(ArrayList<Integer> list, TreeNode root) { if (root == null) { return; } traverse(list, root.left); traverse(list, root.right); list.add(root.val); } }
4-sequence traversal
Sequence traversal of lintcode binary tree BFS layer-by-layer traversal implementation
public class Solution { public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>(); ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); if (root == null) { return list; } queue.offer(root); while (!queue.isEmpty()) { int level = queue.size(); ArrayList<Integer> levelList = new ArrayList<Integer>(); // Layer-by-layer BFS traversal for (int i = 0; i < level; i++) { TreeNode head = queue.poll(); levelList.add(head.val); if (head.left != null) { queue.offer(head.left); } if (head.right != null) { queue.offer(head.right); } } list.add(levelList); } return list; } }