day14 打卡二叉树的递归遍历 迭代遍历 统一迭代
144. 二叉树的前序遍历
1.递归遍历
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorder(root, list);
return list;
}
public void preorder(TreeNode node, List<Integer> list) {
if (node == null) return;
list.add(node.val);
preorder(node.left, list);
preorder(node.right, list);
}
}
2.迭代遍历
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;
stack.push(root);
while (!stack.isEmpty()) {
cur = stack.pop();
list.add(cur.val);
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
return list;
}
}
3.统一迭代
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;
stack.push(root);
while (!stack.isEmpty()) {
cur = stack.peek();
if (cur != null) {
// 弹出节点
stack.pop();
// 右
if (cur.right != null) {
stack.push(cur.right);
}
// 左
if (cur.left != null) {
stack.push(cur.left);
}
// 中
stack.push(cur);
// 标记
stack.push(null);
} else {
// 弹出null
stack.pop();
// 弹出下一个,加入list
list.add(stack.pop().val);
}
}
return list;
}
}
145. 二叉树的后序遍历
1.递归遍历
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
postorder(root, list);
return list;
}
public void postorder(TreeNode node, List<Integer> list) {
if (node == null) return;
postorder(node.left, list);
postorder(node.right, list);
list.add(node.val);
}
}
2.迭代遍历
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;
stack.push(root);
while (!stack.isEmpty()) {
cur = stack.pop();
list.add(cur.val);
if (cur.left != null) {
stack.push(cur.left);
}
if (cur.right != null) {
stack.push(cur.right);
}
}
Collections.reverse(list);
return list;
}
}
3.统一迭代
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;
stack.push(root);
while (!stack.isEmpty()) {
cur = stack.peek();
if (cur != null) {
// 弹出节点
stack.pop();
// 中
stack.push(cur);
stack.push(null);
// 右
if (cur.right != null) stack.push(cur.right);
// 左
if (cur.left != null) stack.push(cur.left);
} else {
// 弹出null
stack.pop();
// 弹出下一个,加入list
list.add(stack.pop().val);
}
}
return list;
}
}
94. 二叉树的中序遍历
94题目链接
1.递归遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorder(root, list);
return list;
}
public void inorder(TreeNode node, List<Integer> list) {
if (node == null) return;
inorder(node.left, list);
list.add(node.val);
inorder(node.right, list);
}
}
2.迭代遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur == null) {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
} else {
stack.push(cur);
cur = cur.left;
}
}
return list;
}
}
3.统一迭代
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;
stack.push(root);
while (!stack.isEmpty()) {
cur = stack.peek();
if (cur == null) {
// 弹出null
stack.pop();
// 弹出下一个,加入list
list.add(stack.pop().val);
} else {
// 弹出节点
stack.pop();
// 右
if (cur.right != null) stack.push(cur.right);
// 中
stack.push(cur);
stack.push(null);
// 左
if (cur.left != null) stack.push(cur.left);
}
}
return list;
}
}