一.二叉树的前序遍历
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> ans = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); while (null != root || !stack.empty()) { while (null != root) { ans.add(root.val); stack.push(root); root = root.left; } TreeNode node = stack.pop(); root = node.right; } return ans; } }
class Solution { List<Integer> ans = null; public List<Integer> preorderTraversal(TreeNode root) { ans = new ArrayList<>(); dfs(root); return ans; } //根左右 void dfs(TreeNode root) { if (null == root) return; ans.add(root.val); dfs(root.left); dfs(root.right); } }
二.二叉树的中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ans = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); while (null != root || !stack.empty()) { while (null != root) { stack.push(root); root = root.left; } TreeNode node = stack.pop(); ans.add(node.val); root = node.right; } return ans; } }
class Solution { List<Integer> ans = null; public List<Integer> inorderTraversal(TreeNode root) { ans = new ArrayList<>(); dfs(root); return ans; } void dfs(TreeNode root) { if (null == root) return; dfs(root.left); ans.add(root.val); dfs(root.right); } }
三.二叉树的后序遍历
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
class Solution { List<Integer> ans = null; public List<Integer> postorderTraversal(TreeNode root) { ans = new ArrayList<>(); dfs(root); return ans; } void dfs(TreeNode root) { if (null == root) return; dfs(root.left); dfs(root.right); ans.add(root.val); } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> ans = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode pre = null; while (null != root || !stack.empty()) { while (null != root) { stack.push(root); root = root.left; } TreeNode node = stack.pop(); if (node.right == null || node.right == pre) { ans.add(node.val); pre = node; } else { stack.push(node); root = node.right; } } return ans; } }
四.二叉树的层次遍历
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ans = new ArrayList<>(); if (null == root) return ans; Queue<TreeNode> Q = new LinkedList<>(); Q.add(root); while (!Q.isEmpty()) { int size = Q.size(); List<Integer> temp = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode node = Q.poll(); temp.add(node.val); if (null != node.left) Q.add(node.left); if (null != node.right) Q.add(node.right); } ans.add(temp); } return ans; } }
五.二叉树的锯齿形层次遍历
https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> ans = new ArrayList<>(); if (null == root) return ans; Queue<TreeNode> Q = new LinkedList<>(); Stack<Integer> stack = new Stack<>(); int flag = 1; Q.add(root); while (!Q.isEmpty()) { int size = Q.size(); List<Integer> temp = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode node = Q.poll(); if (null != node.left) Q.add(node.left); if (null != node.right) Q.add(node.right); if (flag == 1) temp.add(node.val); else stack.push(node.val); } if (flag == 1) ans.add(temp); else { while (!stack.empty()) temp.add(stack.pop()); ans.add(temp); } flag *= -1; } return ans; } }