# 树的遍历

## 前序遍历

### 递归

import java.util.ArrayList;
import java.util.List;

class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root != null) {
}
return list;
}
}


### 迭代

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
// 依次访问左侧链，并将其压入栈中
while (cur != null) {
stack.push(cur); // 入栈
cur = cur.left; // 传递
}
// 对栈中节点的右侧进行迭代
cur = stack.pop();
cur = cur.right;
}
return list;
}
}


import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
return list;
}
}


## 中序遍历

### 递归

import java.util.ArrayList;
import java.util.List;

class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root != null) {
}
return list;
}
}


### 迭代

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
// 将左侧节点依次入栈
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
// 从最左节点开始访问
cur = stack.pop();
// 紧接着对右子树执行此迭代
cur = cur.right;
}
return list;
}
}


import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
return list;
}
}


## 后序遍历

### 递归

import java.util.ArrayList;
import java.util.List;

class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root != null) {
}
return list;
}
}


### 迭代

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
TreeNode p = null;//用来记录上一节点
while (!stack.isEmpty() || cur != null) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.peek();
// 后序遍历的过程中在遍历完左子树跟右子树cur都会回到根结点。
// 所以当前不管是从左子树还是右子树回到根结点都不应该再操作了，应该退回上层。
// 如果是从右边再返回根结点，应该回到上层。
// 主要就是判断出来的是不是右子树，是的话就可以把根节点=加入到list了
if (cur.right == null || cur.right == p) {
stack.pop();
p = cur;
cur = null;
} else {
cur = cur.right;
}
}
return list;
}
}


import java.util.Deque;
import java.util.List;

class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
Deque<TreeNode> stack = new LinkedList<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.right;
} else {
root = stack.pop();
root = root.left;
}
}
return list;
}
}


## 层序遍历

### 迭代

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
deque.offerLast(root);
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> list = new LinkedList<>();
while (size-- > 0) {
TreeNode treeNode = deque.pollFirst();
if (treeNode == null) continue;