Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

    • 组合类型
    • 子集类型
    • 排列类型
    • 树回溯
      • 257. 二叉树的所有路径
      • 112. 路径总和
      • 113. 路径总和 II
      • 129. 求根节点到叶节点数字之和
      • 437. 路径总和 III
    • 经典题
    • 遍历dfs
  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 回溯
Nreal
2023-11-19
目录

树回溯

# 257. 二叉树的所有路径 (opens new window)

class Solution {
    List<String> ret = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root,new StringBuilder());
        return ret;
    }
    void dfs(TreeNode node,StringBuilder path){
        if(node.left==null && node.right==null){
            ret.add(path.toString()+node.val);
            return ;
        }
        // 想想这个为什么放在判断语句下面?
        int start = path.length();
        path.append(node.val).append("->");
        if(node.left!=null)
            dfs(node.left,path);
        if(node.right!=null)
            dfs(node.right,path);
        path.delete(start,path.length());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 112. 路径总和 (opens new window)

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root==null) return false;
        return dfs(root,targetSum);
    }
    boolean dfs(TreeNode root,int targetSum){
        if(root.left==null && root.right==null &&
        targetSum-root.val==0){
            return true;
        }
        // 选左边
        if(root.left!=null){
            if(dfs(root.left,targetSum-root.val)){
                return true;
            }
        }
        // 选右边
        if(root.right!=null){
            if(dfs(root.right,targetSum-root.val)){
                return true;
            }
        }
        return false;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 113. 路径总和 II (opens new window)

class Solution {
    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root==null) return ret;
        dfs(root,targetSum);
        return ret;
    }
    void dfs(TreeNode root,int targetSum){
        path.add(root.val);
        if(root.left==null && root.right==null &&
        targetSum-root.val==0){
            ret.add(new ArrayList<>(path));
            return ;
        }
        if(root.left!=null){
            dfs(root.left,targetSum-root.val);
            path.remove(path.size()-1);
        }
        if(root.right!=null){
            dfs(root.right,targetSum-root.val);
            path.remove(path.size()-1);
        }
        return ;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 129. 求根节点到叶节点数字之和 (opens new window)

class Solution {
    StringBuilder path = new StringBuilder();
    int ret;
    public int sumNumbers(TreeNode root) {
        dfs(root);
        return ret;
    }
    void dfs(TreeNode root){
        if(root==null) return ;
        path.append(root.val);
        if(root.left==null && root.right==null){
            ret += Integer.parseInt(path.toString());
        }
        if(root.left!=null){
            dfs(root.left);
            path.deleteCharAt(path.length()-1);
        }
        if(root.right!=null){
            dfs(root.right);
            path.deleteCharAt(path.length()-1);
        }
        return ;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 437. 路径总和 III (opens new window)

class Solution {
    int ret = 0;
    public int pathSum(TreeNode root, int targetSum) {
        if(root==null)
            return 0;
        dfs(root,targetSum);
        pathSum(root.left,targetSum);
        pathSum(root.right,targetSum);
        return ret;
    }
    void dfs(TreeNode node,int targetSum){
        if(node==null)
            return ;
        // 简单可以这么写,但是其它地方都不要再-node.val了;
        //targetSum -= node.val;
        if(targetSum-node.val==0)
            ret++;
        if(node.left!=null)
            dfs(node.left,targetSum-node.val);
        if(node.right!=null)
            dfs(node.right,targetSum-node.val);
        // 简单写这句不用加,压根不会执行这句话
        //target += node.val
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
排列类型
经典题

← 排列类型 经典题→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式