1. 树

1.1 二叉树前序

  • 递归

    1. public List<Integer> preorderTraversal(TreeNode root) {
    2. List<Integer> resultList = new ArrayList<>();
    3. if (root == null) {
    4. return resultList;
    5. }
    6. resultList.add(root.val);
    7. if (root.left != null) {
    8. resultList.addAll(preorderTraversal(root.left));
    9. }
    10. if (root.right != null) {
    11. resultList.addAll(preorderTraversal(root.right));
    12. }
    13. return resultList;
    14. }
    1. public List<Integer> preorderTraversal(TreeNode root) {
    2. List<Integer> resultList = new ArrayList<Integer>();
    3. if (root == null) {
    4. return resultList;
    5. }
    6. Deque<TreeNode> stack = new LinkedList<TreeNode>();
    7. TreeNode node = root;
    8. while (!stack.isEmpty() || node != null) {
    9. while (node != null) {
    10. resultList.add(node.val);
    11. stack.push(node);
    12. node = node.left;
    13. }
    14. node = stack.pop();
    15. node = node.right;
    16. }
    17. return resultList;
    18. }

    1.2 二叉树中序

  • 递归

    1. public List<Integer> preorderTraversal(TreeNode root) {
    2. List<Integer> resultList = new ArrayList<>();
    3. if (root == null) {
    4. return resultList;
    5. }
    6. if (root.left != null) {
    7. resultList.addAll(preorderTraversal(root.left));
    8. }
    9. resultList.add(root.val);
    10. if (root.right != null) {
    11. resultList.addAll(preorderTraversal(root.right));
    12. }
    13. return resultList;
    14. }
    1. public List<Integer> inorderTraversal(TreeNode root) {
    2. List<Integer> resultList = new ArrayList<Integer>();
    3. Deque<TreeNode> stack = new LinkedList<TreeNode>();
    4. while (root != null || !stack.isEmpty()) {
    5. while (root != null) {
    6. stack.push(root);
    7. root = root.left;
    8. }
    9. root = stack.pop();
    10. resultList.add(root.val);
    11. root = root.right;
    12. }
    13. return resultList;
    14. }

    1.3 二叉树后序

    1. public List<Integer> preorderTraversal(TreeNode root) {
    2. List<Integer> resultList = new ArrayList<>();
    3. if (root == null) {
    4. return resultList;
    5. }
    6. if (root.left != null) {
    7. resultList.addAll(preorderTraversal(root.left));
    8. }
    9. if (root.right != null) {
    10. resultList.addAll(preorderTraversal(root.right));
    11. }
    12. resultList.add(root.val);
    13. return resultList;
    14. }
  • 栈 ```java

    public List postorderTraversal(TreeNode root) {

    1. List<Integer> resultList = new ArrayList<Integer>();
    2. if (root == null) {
    3. return resultList;
    4. }
    5. Deque<TreeNode> stack = new LinkedList<TreeNode>();
    6. TreeNode prev = null;
    7. while (root != null || !stack.isEmpty()) {
    8. while (root != null) {
    9. stack.push(root);
    10. root = root.left;
    11. }
    12. root = stack.pop();
    13. if (root.right == null || root.right == prev) {
    14. resultList.add(root.val);
    15. prev = root;
    16. root = null;
    17. } else {
    18. stack.push(root);
    19. root = root.right;
    20. }
    21. }
    22. return resultList;

    }

  1. <a name="SKCie"></a>
  2. ### 1.4 N叉树前序
  3. ```java
  4. public List<Integer> preorder(Node root) {
  5. List<Integer> resultList = new ArrayList<>();
  6. if (null == root) {
  7. return resultList;
  8. }
  9. resultList.add(root.val);
  10. if (null != root.children && root.children.size() > 0) {
  11. for (Node temp : root.children) {
  12. resultList.addAll(preorder(temp));
  13. }
  14. }
  15. return resultList;
  16. }

1.5 N叉树后序

  1. public List<Integer> preorder(Node root) {
  2. List<Integer> resultList = new ArrayList<>();
  3. if (null == root) {
  4. return resultList;
  5. }
  6. if (null != root.children && root.children.size() > 0) {
  7. for (Node temp : root.children) {
  8. resultList.addAll(preorder(temp));
  9. }
  10. }
  11. resultList.add(root.val);
  12. return resultList;
  13. }

1.6 N叉树层次遍历

  1. public List<List<Integer>> levelOrder(Node root) {
  2. List<List<Integer>> resultList = new ArrayList<>();
  3. if (root == null) {
  4. return resultList;
  5. }
  6. Queue<Node> queue = new LinkedList<>();
  7. queue.add(root);
  8. while (!queue.isEmpty()) {
  9. List<Integer> level = new ArrayList<>();
  10. int size = queue.size();
  11. for (int i = 0; i < size; i++) {
  12. Node node = queue.poll();
  13. level.add(node.val);
  14. queue.addAll(node.children);
  15. }
  16. resultList.add(level);
  17. }
  18. return resultList;
  19. }

1.7 从前序与中序遍历序列构造二叉树(105)

1.8 路径总和 II (113)

2. 递归

2.0 递归模版

  1. public void recursion(int level, Object params) {
  2. // 递归终止
  3. if (level > MAX_LEVEL) {
  4. // proccess result...
  5. return;
  6. }
  7. // do some work.
  8. process(level, params);
  9. // recursion
  10. recursion(level, newParams);
  11. // if needed, update status.
  12. }

2.1 爬楼梯(70)

  • 解法1: 递归分解子问题

    1. public int climbStairs(int n) {
    2. if (n <= 2) {
    3. return n;
    4. }
    5. return climbStairs( n - 1 ) + climbStairs( n - 2 );
    6. }
  • 记录前两个值:类似于斐波那契饿数列

    1. public int climbStairs(int n) {
    2. if (n <= 2) {
    3. return n;
    4. }
    5. int a =1 ,b =2 ,c = 3;
    6. for (int i = 3; i <= n; i++) {
    7. c = a + b;
    8. a = b;
    9. b = c;
    10. }
    11. return c;
    12. }

    2.2 括号生成(22)

    1. public List<String> generateParenthesis(int n) {
    2. List<String> resultList = new ArrayList<>();
    3. recursion(0, 0, n, "", resultList);
    4. return resultList;
    5. }
    6. private void recursion(int left, int right, int n, String s, List<String> resultList) {
    7. if (left == n && left == right) {
    8. resultList.add(s);
    9. return;
    10. }
    11. if (left < n) {
    12. recursion(left + 1, right, n, s + "(", resultList);
    13. }
    14. if (right < left) {
    15. recursion(left, right + 1, n, s + ")", resultList);
    16. }
    17. }