基础算法和数据结构高频题 II
DFS的两种理解方式:
1. 按照实际执行顺序模拟 (适合枚举型DFS,下节课内容)
2. 按照DFS的定义宏观理解 (适合分治型DFS,本节课内容)
1 Convert BST to Greater Tree
int sum = 0; public TreeNode convertBST(TreeNode root) { dfs(root); return root; } void dfs(TreeNode root) { if (root == null) { return; } dfs(root.right); sum = sum + root.val; root.val = sum; dfs(root.left); }
2 Inorder Successor in Binary Search Tree
public TreeNode InorderSuccessor(TreeNode root, TreeNode p) { if (root == null || p == null) { return null; } if (root.val <= p.val) { return InorderSuccessor(root.right, p); } else { TreeNode left = InorderSuccessor(root.left, p); return left == null ? root, left; } }
3 Validate Binary Search Tree
public boolean isValidBST(TreeNode root) { return help(root, Long.MIN_VALUE, Long.MAX_VALUE); } boolean help(TreeNode root, long min, long max) { if (root == null) { return true; } if (root.val <= min || root.val >= max) { return false; } return help(root.left, min, root.val) && help(root.right, root.val, max); }
4Binary Tree Inorder Traversal
public class Solution { /** * @param root: The root of binary tree. * @return: Inorder in ArrayList which contains node values. */ public ArrayList<Integer> inorderTraversal(TreeNode root) { // write your code here ArrayList<Integer> res = new ArrayList<>(); dfs(root, res); return res; } void dfs(TreeNode root, ArrayList<Integer> res) { if (root == null) { return; } dfs(root.left, res); res.add(root.val); dfs(root.right, res); } }
二叉树类问题
5 Binary Tree Flipping
TreeNode newRoot; void dfs(TreeNode cur) { if (cur.left == null) { newRoot = cur; return; } dfs(cur.left); cur.left.right = cur; cur.left.left = cur.left; cur.left = null; cur.right = null; } public TreeNodee upsideDownBinaryTree(TreeNode root) { if (root == null) { return root; } dfs(root); return newRoot; }
6 Binary Tree Leaves Order Traversal
Map<Integer, List<Integer>> map = new HashMap<>(); int dfs(TreeNode root) { if (root == null) { return 0; } int left = dfs(root.left); int right = dfs(root.right); int max = Math.max(left, right) + 1; if (!map.containsKey(max)) { map.put(max, new ArrayList<>()); } map.get(max).add(root.val); return max; } public List<List<Integer>> findLeaves(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) { return res; } int max_deep = dfs(root); for (int i = 1; i <= max_deep; i++) { res.add(map.get(i)); } return res; }
7Binary Tree Leaves Order Traversal
public List<List<Integer>> virtalOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) { return res; } Map<Integer, ArrayList<Integer>> map = new HashMap<>(); Queue<Integer> c = new LinkedList<>(); Queue<TreeNode> q = new LinkedList<>(); c.offer(0); q.offer(root); while (!q.isEmpty()) { Integer l = c.poll(); TreeNode node = q.poll(); if (!map.containsKey(l)) { map.put(l, new ArrayList<>()); } map.get(l).add(node.val); if (node.left != null) { c.offer(l - 1); q.offer(node.left); } if (node.right != null) { c.offer(l + 1); q.offer(node.right); } } for (int i = Collections.min(map.keySet()); i <= Collections.max(map.keySet()); i++) { res.add(map.get(i)); } return res; }