1.二叉树的中序遍历
Leetcode 94;medium;
遍历系列常使用,最好能掌握迭代和递归方法!
package tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Main0094二叉树的中序遍历 {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
TreeNode node1 = new TreeNode(2);
TreeNode node2 = new TreeNode(3);
root.left = node1;
node1.right = node2;
List<Integer> list = new Solution094().inorderTraversal(root);
System.out.println(list);
}
}
// 迭代
class Solution094 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> output = new ArrayList<>();
if (root == null)
return output;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
output.add(cur.val);
cur = cur.right;
}
return output;
}
}
// 递归
class Solution94 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorderTraversal(root, list);
return list;
}
public void inorderTraversal(TreeNode root, List<Integer> list) {
if (root == null)
return;
inorderTraversal(root.left, list);
list.add(root.val);
inorderTraversal(root.right, list);
}
}
class Solution0094 {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null)
return list;
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
return list;
}
}
2.二叉树的前序遍历
Leetcode 144;medium;
前序遍历和中序遍历很想像,同样需要掌握递归和迭代方法!
package tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
// 迭代
class Solution0144 {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> output = new ArrayList<>();
if (root == null)
return output;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
output.add(cur.val);
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return output;
}
}
// 递归
class Solution144 {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorderTraversal(root, list);
return list;
}
public void preorderTraversal(TreeNode root, List<Integer> list) {
if (root == null)
return;
list.add(root.val);
preorderTraversal(root.left, list);
preorderTraversal(root.right, list);
}
}
3.二叉树的后序遍历
Leetcode 145;medium;
后序遍历和前中遍历稍有区别,同样需要掌握递归和迭代!
package tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
class Solution145 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> output = new ArrayList<>();
if (root == null)
return output;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
output.add(0, cur.val);
if (cur.left != null)
stack.push(cur.left);
if (cur.right != null)
stack.push(cur.right);
}
return output;
}
}
class Solution0145 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
postorderTraversal(root, list);
return list;
}
public void postorderTraversal(TreeNode root, List<Integer> list) {
if (root == null)
return;
postorderTraversal(root.left, list);
list.add(root.val);
postorderTraversal(root.right, list);
}
}
4.二叉树的层序遍历
Leetcode 102;medium;
层序遍历很重要,并且延伸出来的问题很多。比如: 107自底向上遍历、103锯齿遍历(ZigZag)、515层最大值、637层平均值、199二叉树的右视图等,为了不显得代码冗余,此题只给出一个层序遍历。
package tree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Main0102二叉树的层次遍历 {
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
TreeNode node1 = new TreeNode(9);
TreeNode node2 = new TreeNode(20);
TreeNode node3 = new TreeNode(15);
TreeNode node4 = new TreeNode(7);
root.left = node1;
root.right = node2;
node2.left = node3;
node2.right = node4;
List<List<Integer>> list = new Solution102().levelOrder(root);
for (List<Integer> list2 : list) {
System.out.println(list2);
}
}
}
// 递归
class Solution102 {
List<List<Integer>> output = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null)
return output;
levelOrder(root, 0);
return output;
}
public void levelOrder(TreeNode root, int level) {
if (output.size() == level)
output.add(new ArrayList<>());
output.get(level).add(root.val);
if (root.left != null)
levelOrder(root.left, level + 1);
if (root.right != null)
levelOrder(root.right, level + 1);
}
}
// 迭代
class Solution0102 {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> output = new ArrayList<>();
if (root == null)
return output;
Queue<TreeNode> queue = new LinkedList<>();// 注
queue.add(root);
int level = 0;
while (!queue.isEmpty()) {
output.add(new ArrayList<>());
int length = queue.size();
for (int i = 0; i < length; i++) {
TreeNode cur = queue.poll();
output.get(level).add(cur.val);
if (cur.left != null)
queue.add(cur.left);
if (cur.right != null)
queue.add(cur.right);
}
level++;
}
return output;
}
}