x// 迭代
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
res.add(root.val);
root = root.left;
}
TreeNode cur = stack.pop();
root = cur.right;
}
return res;
}
// 递归 1:分解子问题
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
res.add(root.val);
res.addAll(preorderTraversal(root.left));
res.addAll(preorderTraversal(root.right));
return res;
}
// 递归 2:遍历思维
List<Integer> res = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
traversal(root);
return res;
}
private void traversal(TreeNode root) {
if (root == null) return ;
res.add(root.val);
traversal(root.left);
traversal(root.right);
}
xxxxxxxxxx
// 迭代
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
TreeNode cur = stack.pop();
res.add(cur.val);
root = cur.right;
}
return res;
}
// 递归 1:分解子问题
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
res.addAll(inorderTraversal(root.left));
res.add(root.val);
res.addAll(inorderTraversal(root.right));
return res;
}
// 递归 2:遍历思维
List<Integer> res = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
traversal(root);
return res;
}
private void traversal(TreeNode root) {
if (root == null) return ;
traversal(root.left);
res.add(root.val);
traversal(root.right);
}
xxxxxxxxxx
// 迭代
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
if (root.left != null) {
root = root.left;
} else {
root = root.right;
}
}
TreeNode cur = stack.pop();
res.add(cur.val);
if (!stack.isEmpty() && stack.peek().left == cur) {
root = stack.peek().right;
}
}
return res;
}
// 递归 1:分解子问题
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
res.addAll(postorderTraversal(root.left));
res.addAll(postorderTraversal(root.right));
res.add(root.val);
return res;
}
// 递归 2:遍历思维
List<Integer> res = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
traversal(root);
return res;
}
private void traversal(TreeNode root) {
if (root == null) return ;
traversal(root.left);
traversal(root.right);
res.add(root.val);
}
xxxxxxxxxx
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode cur = q.poll();
list.add(cur.val);
if (cur.left != null) q.offer(cur.left);
if (cur.right != null) q.offer(cur.right);
}
res.add(list);
}
return res;
}
今天突然看到一个新的遍历形式 垂直遍历 大千世界 无奇不有
xxxxxxxxxx
class Solution {
// 存放 节点 && 行 && 列 的信息
class Triple {
TreeNode node;
int row; // 行
int col; // 列
public Triple(TreeNode node, int row, int col) {
this.node = node;
this.row = row;
this.col = col;
}
}
private List<Triple> nodes;
public List<List<Integer>> verticalTraversal(TreeNode root) {
nodes = new ArrayList<>();
// 遍历出所有节点
traversal(root, 0, 0);
// 按照一定的规则排序
nodes.sort((t1, t2) -> {
// 行列相等,则按照 val 从小到大排序
if (t1.row == t2.row && t1.col == t2.col) {
return t1.node.val - t2.node.val;
}
// 如果仅仅列相等,按照行从上到下排序
if (t1.col == t2.col) {
return t1.row - t2.row;
}
// other:按照列从前到后排序
return t1.col - t2.col;
});
List<List<Integer>> res = new ArrayList<>();
int preCol = Integer.MIN_VALUE;
for (Triple t : nodes) {
if (preCol != t.col) {
preCol = t.col;
res.add(new ArrayList<Integer>());
}
res.get(res.size() - 1).add(t.node.val);
}
return res;
}
private void traversal(TreeNode root, int row, int col) {
if (root == null) {
return ;
}
nodes.add(new Triple(root, row, col));
traversal(root.left, row + 1, col - 1);
traversal(root.right, row + 1, col + 1);
}
}