文章均为本人技术笔记,转载请注明出处:
[1] https://segmentfault.com/u/yzwall
[2] blog.csdn.net/j_dark/
二叉树遍历:按照既定序,对每个节点仅访问一次;
二叉树非递归遍历思想:参考这篇博文,核心思想是存在重合元素的局部有序保证整体有序,由于二叉树的结构特点,二叉树中的每个节点(除根节点和叶子节点)均属于两个局部的重合元素。对于任一重合元素,保证所在两个局部遍历有序,保证实现整体遍历有序;
二叉树节点TreeNode
声明
public class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
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 preorderTraversal(TreeNode root) {
ArrayList list = new ArrayList();
if (root == null) {
return list;
}
ArrayDeque stack = new ArrayDeque();
stack.push(new Pair(root, false));
while (!stack.isEmpty()) {
Pair top = stack.pop();
// 重合节点完成所有局部有序,弹出
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;
}
}
public class Solution {
public ArrayList preorderTraversal(TreeNode root) {
ArrayList list = new ArrayList();
if (root == null) {
return list;
}
traverse(list, root);
return list;
}
private void traverse(ArrayListlist, TreeNode root) {
if (root == null) {
return;
}
list.add(root.val);
traverse(list, root.left);
traverse(list, root.right);
}
}
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 inorderTraversal(TreeNode root) {
ArrayList list = new ArrayList();
if (root == null) {
return list;
}
ArrayDeque stack = new ArrayDeque();
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;
}
}
public class Solution {
public ArrayList inorderTraversal(TreeNode root) {
ArrayList list = new ArrayList();
if (root == null) {
return list;
}
traverse(list, root);
return list;
}
private void traverse(ArrayListlist, TreeNode root) {
if (root == null) {
return;
}
traverse(list, root.left);
list.add(root.val);
traverse(list, root.right);
}
}
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 postorderTraversal(TreeNode root) {
ArrayList list = new ArrayList();
if (root == null) {
return list;
}
ArrayDeque stack = new ArrayDeque();
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;
}
}
public class Solution {
public ArrayList postorderTraversal(TreeNode root) {
ArrayList list = new ArrayList();
if (root == null) {
return list;
}
traverse(list, root);
return list;
}
private void traverse(ArrayList list, TreeNode root) {
if (root == null) {
return;
}
traverse(list, root.left);
traverse(list, root.right);
list.add(root.val);
}
}
public class Solution {
public ArrayList> levelOrder(TreeNode root) {
ArrayDeque queue = new ArrayDeque();
ArrayList> list = new ArrayList>();
if (root == null) {
return list;
}
queue.offer(root);
while (!queue.isEmpty()) {
int level = queue.size();
ArrayList levelList = new ArrayList();
// 按层BFS遍历
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;
}
}