二叉树中和为某一值的路径

开发 前端
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。难度:中等叶子节点 是指没有子节点的节点。

[[438573]]

Leetcode : https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof

“GitHub : https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_27_pathSum/Solution.java

二叉树中和为某一值的路径

“题目描述 :给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。难度:中等叶子节点 是指没有子节点的节点。示例 1:

  1. 输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 
  2.  
  3. 输出:[[5,4,11,2],[5,8,4,5]] 

示例 2:

 

  1. 输入:root = [1,2,3], targetSum = 5 
  2.  
  3. 输出:[] 

示例 3:

  1. 输入:root = [1,2], targetSum = 0 
  2.  
  3. 输出:[] 

解题思路:

“本问题是典型的二叉树方案搜索问题,使用回溯法解决,其包含 先序遍历 + 路径记录 两部分。

  • 先序遍历: 按照 “根、左、右” 的顺序,遍历树的所有节点。
  • 路径记录: 在先序遍历中,记录从根节点到当前节点的路径。当路径为 ① 根节点到叶节点形成的路径 且 ② 各节点值的和等于目标值 sum 时,将此路径加入结果列表。

算法流程:pathSum(root,sum) 函数:

  • 初始化:结果列表res,路径列表 path.
  • 返回值:返回res即可。

recur (root, tar) 函数:

  • 递推参数:当前节点root,当前目标值tar 。
  • 终止条件:若节点root为空,则直接返回。
  • 递推工作:
    • 路径更新:将当前节点值root. val加入路径path ;
    • 目标值更新:tar=tar - root.val(即目标值tar从sum减至0);
    • 路径记录:当①root为叶节点且②路径和等于目标值,则将此路径path加入res。
    • 先序遍历:递归左1右子节点。
    • 路径恢复:向上回溯前,需要将当前节点从路径path中删除,即执行path. pop()。

复杂度分析:

  • 时间复杂度 O(N): N 为二叉树的节点数,先序遍历需要遍历所有节点。
  • 空间复杂度 O(N): 最差情况下,即树退化为链表时,path 存储所有树节点,使用 O(N) 额外空间。
  1. package com.nateshao.sword_offer.topic_27_pathSum; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.LinkedList; 
  5. import java.util.List; 
  6.  
  7. /** 
  8.  * @date Created by 邵桐杰 on 2021/12/1 16:45 
  9.  * @微信公众号 程序员千羽 
  10.  * @个人网站 www.nateshao.cn 
  11.  * @博客 https://nateshao.gitee.io 
  12.  * @GitHub https://github.com/nateshao 
  13.  * @Gitee https://gitee.com/nateshao 
  14.  * Description: 二叉树中和为某一值的路径 
  15.  */ 
  16. public class Solution { 
  17.  
  18.     public static void main(String[] args) { 
  19.         TreeNode node1 = new TreeNode(10); 
  20.         TreeNode node2 = new TreeNode(5); 
  21.         TreeNode node3 = new TreeNode(12); 
  22.         TreeNode node4 = new TreeNode(4); 
  23.         TreeNode node5 = new TreeNode(7); 
  24.  
  25.         node1.left = node2; 
  26.         node1.right = node3; 
  27.         node2.left = node4; 
  28.         node2.right = node5; 
  29.  
  30.         Solution p = new Solution(); 
  31.         System.out.println(p.listAll); 
  32.         p.pathSum(node1, 22); 
  33.         for (List<Integer> list : p.listAll) { 
  34.             for (int i : list) { 
  35.                 System.out.print(i + " "); 
  36.             } 
  37.             System.out.println(); 
  38.         } 
  39.         /** 
  40.          * [] 
  41.          * 10 5 7 
  42.          * 10 12 
  43.          */ 
  44.     } 
  45.  
  46.     /****************************** 剑指offer **********************************/ 
  47.  
  48.     private static List<List<Integer>> listAll = new ArrayList<>(); 
  49.     private static List<Integer> list = new ArrayList<>(); 
  50.  
  51.     /** 
  52.      * 思路:先保存根节点,然后分别递归在左右子树中找目标值,若找到即到达叶子节点,打印路径中的值 
  53.      * 
  54.      * @param root 
  55.      * @param target 
  56.      * @return 
  57.      */ 
  58.     public static List<List<Integer>> pathSum(TreeNode root, int target) { 
  59.         if (root == nullreturn listAll; 
  60.         list.add(root.val); 
  61.         target -= root.val; 
  62.         if (target == 0 && root.left == null && root.right == null) { 
  63.             listAll.add(new ArrayList<>(list)); 
  64.         } 
  65.         pathSum(root.left, target); 
  66.         pathSum(root.right, target); 
  67.         list.remove(list.size() - 1); 
  68.         return listAll; 
  69.     } 
  70.  
  71.  
  72.     /*********************** 精选解答 ***************************/ 
  73.     LinkedList<List<Integer>> res = new LinkedList<>(); 
  74.     LinkedList<Integer> path = new LinkedList<>(); 
  75.  
  76.     public List<List<Integer>> pathSum2(TreeNode root, int sum) { 
  77.         recur(root, sum); 
  78.         return res; 
  79.     } 
  80.  
  81.     void recur(TreeNode root, int tar) { 
  82.         if (root == nullreturn
  83.         path.add(root.val); 
  84.         tar -= root.val; 
  85.         if (tar == 0 && root.left == null && root.right == null
  86.             res.add(new LinkedList(path)); 
  87.         recur(root.left, tar); 
  88.         recur(root.right, tar); 
  89.         path.removeLast(); 
  90.     } 
  91.  
  92.  
  93.     public static class TreeNode { 
  94.         int val; 
  95.         TreeNode left
  96.         TreeNode right
  97.  
  98.         TreeNode() { 
  99.         } 
  100.  
  101.         TreeNode(int val) { 
  102.             this.val = val; 
  103.         } 
  104.  
  105.         TreeNode(int val, TreeNode left, TreeNode right) { 
  106.             this.val = val; 
  107.             this.left = left
  108.             this.right = right
  109.         } 
  110.     } 

 

责任编辑:武晓燕 来源: 程序员千羽
相关推荐

2022-11-06 19:43:10

二叉树指针节点

2020-04-27 07:05:58

二叉树左子树右子树

2021-05-06 17:46:30

二叉树数据结构

2021-04-20 08:37:14

数据结构二叉树

2021-04-19 07:47:42

数据结构二叉树Tree

2021-08-06 11:34:05

二叉树递归回溯

2021-11-29 10:40:58

二叉树镜像节点

2021-04-28 20:12:27

数据结构创建

2022-10-26 23:58:02

二叉树数组算法

2013-07-15 16:35:55

二叉树迭代器

2021-03-17 08:19:22

二叉树LeetCode

2021-08-27 11:36:44

二叉树回溯节点

2021-12-17 14:26:58

二叉树节点数量

2021-09-29 10:19:00

算法平衡二叉树

2020-09-23 18:25:40

算法二叉树多叉树

2022-07-27 07:45:53

二叉树镜像函数

2018-03-15 08:31:57

二叉树存储结构

2022-10-12 23:25:17

二叉树父节点根节点

2021-10-12 09:25:11

二叉树树形结构

2021-09-15 07:56:32

二叉树层次遍历
点赞
收藏

51CTO技术栈公众号