1、二叉树的前序遍历
public List<Integer> preorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
preorderTraversal(root, res);
return res;
}
private void preorderTraversal(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
res.add(root.val);
preorderTraversal(root.left, res);
preorderTraversal(root.right, res);
}
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
LinkedList<TreeNode> stack = new LinkedList<>();
TreeNode curr = root;
while (!stack.isEmpty() || curr != null) {
while (curr != null) {
res.add(curr.val);
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
curr = curr.right;
}
return res;
}
2、二叉树的中序遍历
public List<Integer> inorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
preorderTraversal(root, res);
return res;
}
private void inorderTraversal(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
inorderTraversal(root.left, res);
res.add(root.val);
inorderTraversal(root.right, res);
}
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
LinkedList<TreeNode> stack = new LinkedList<>();
TreeNode curr = root;
while (!stack.isEmpty() || curr != null) {
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
res.add(curr.val);
curr = curr.right;
}
return res;
}
3、二叉树的后序遍历
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
preorderTraversal(root, res);
return res;
}
private void postorderTraversal(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
postorderTraversal(root.left, res);
postorderTraversal(root.right, res);
res.add(root.val);
}
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
LinkedList<TreeNode> stack = new LinkedList<>();
TreeNode curr = root;
while (!stack.isEmpty() || curr != null) {
while (curr != null) {
res.addFirst(curr.val);
stack.push(curr);
curr = curr.right;
}
TreeNode node = stack.pop();
curr = node.left;
}
return res;
}
4、二叉树的层次遍历
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if(root==null){
return result;
}
handle(root,0,result);
return result;
}
private void handle(TreeNode root, int level,List<List<Integer>> result){
if(root == null){
return;
}
if(result.size()<level+1){
result.add(new ArrayList<>());
}
result.get(level).add(root.val);
handle(root.left,level+1,result);
handle(root.right,level+1,result);
}
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.add(root);
}
while (!queue.isEmpty()) {
int n = queue.size();
List<Integer> level = new ArrayList<>();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res.add(level);
}
return res;
}
5、二叉树的之形(锯齿形)遍历
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new LinkedList<>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> te = new LinkedList<>();
for(int i = 0;i<size;i++){
TreeNode node = queue.poll();
if(level % 2 ==0){
te.add(node.val);
}else{
te.addFirst(0,node.val);
}
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
res.add(te);
level++;
}
return res;
}