自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

二叉樹中和為某一值的路徑

開發(fā) 前端
給你二叉樹的根節(jié)點(diǎn) root 和一個(gè)整數(shù)目標(biāo)和 targetSum ,找出所有 從根節(jié)點(diǎn)到葉子節(jié)點(diǎn) 路徑總和等于給定目標(biāo)和的路徑。難度:中等葉子節(jié)點(diǎn) 是指沒有子節(jié)點(diǎn)的節(jié)點(diǎn)。

[[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

二叉樹中和為某一值的路徑

“題目描述 :給你二叉樹的根節(jié)點(diǎn) root 和一個(gè)整數(shù)目標(biāo)和 targetSum ,找出所有 從根節(jié)點(diǎn)到葉子節(jié)點(diǎn) 路徑總和等于給定目標(biāo)和的路徑。難度:中等葉子節(jié)點(diǎn) 是指沒有子節(jié)點(diǎn)的節(jié)點(diǎn)。示例 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. 輸出:[] 

解題思路:

“本問題是典型的二叉樹方案搜索問題,使用回溯法解決,其包含 先序遍歷 + 路徑記錄 兩部分。

  • 先序遍歷: 按照 “根、左、右” 的順序,遍歷樹的所有節(jié)點(diǎn)。
  • 路徑記錄: 在先序遍歷中,記錄從根節(jié)點(diǎn)到當(dāng)前節(jié)點(diǎn)的路徑。當(dāng)路徑為 ① 根節(jié)點(diǎn)到葉節(jié)點(diǎn)形成的路徑 且 ② 各節(jié)點(diǎn)值的和等于目標(biāo)值 sum 時(shí),將此路徑加入結(jié)果列表。

算法流程:pathSum(root,sum) 函數(shù):

  • 初始化:結(jié)果列表res,路徑列表 path.
  • 返回值:返回res即可。

recur (root, tar) 函數(shù):

  • 遞推參數(shù):當(dāng)前節(jié)點(diǎn)root,當(dāng)前目標(biāo)值tar 。
  • 終止條件:若節(jié)點(diǎn)root為空,則直接返回。
  • 遞推工作:
    • 路徑更新:將當(dāng)前節(jié)點(diǎn)值root. val加入路徑path ;
    • 目標(biāo)值更新:tar=tar - root.val(即目標(biāo)值tar從sum減至0);
    • 路徑記錄:當(dāng)①root為葉節(jié)點(diǎn)且②路徑和等于目標(biāo)值,則將此路徑path加入res。
    • 先序遍歷:遞歸左1右子節(jié)點(diǎn)。
    • 路徑恢復(fù):向上回溯前,需要將當(dāng)前節(jié)點(diǎn)從路徑path中刪除,即執(zhí)行path. pop()。

復(fù)雜度分析:

  • 時(shí)間復(fù)雜度 O(N): N 為二叉樹的節(jié)點(diǎn)數(shù),先序遍歷需要遍歷所有節(jié)點(diǎn)。
  • 空間復(fù)雜度 O(N): 最差情況下,即樹退化為鏈表時(shí),path 存儲(chǔ)所有樹節(jié)點(diǎn),使用 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.  * @微信公眾號(hào) 程序員千羽 
  10.  * @個(gè)人網(wǎng)站 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.      * 思路:先保存根節(jié)點(diǎn),然后分別遞歸在左右子樹中找目標(biāo)值,若找到即到達(dá)葉子節(jié)點(diǎn),打印路徑中的值 
  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.     } 

 

責(zé)任編輯:武曉燕 來源: 程序員千羽
相關(guān)推薦

2022-11-06 19:43:10

二叉樹指針節(jié)點(diǎn)

2020-04-27 07:05:58

二叉樹左子樹右子樹

2021-05-06 17:46:30

二叉樹數(shù)據(jù)結(jié)構(gòu)

2021-04-19 07:47:42

數(shù)據(jù)結(jié)構(gòu)二叉樹Tree

2021-04-20 08:37:14

數(shù)據(jù)結(jié)構(gòu)二叉樹

2021-04-28 20:12:27

數(shù)據(jù)結(jié)構(gòu)創(chuàng)建

2022-10-26 23:58:02

二叉樹數(shù)組算法

2021-03-17 08:19:22

二叉樹LeetCode

2013-07-15 16:35:55

二叉樹迭代器

2021-11-29 10:40:58

二叉樹鏡像節(jié)點(diǎn)

2021-08-27 11:36:44

二叉樹回溯節(jié)點(diǎn)

2021-08-06 11:34:05

二叉樹遞歸回溯

2021-09-29 10:19:00

算法平衡二叉樹

2021-12-17 14:26:58

二叉樹節(jié)點(diǎn)數(shù)量

2020-09-23 18:25:40

算法二叉樹多叉樹

2022-07-27 07:45:53

二叉樹鏡像函數(shù)

2018-03-15 08:31:57

二叉樹存儲(chǔ)結(jié)構(gòu)

2021-10-12 09:25:11

二叉樹樹形結(jié)構(gòu)

2021-09-15 07:56:32

二叉樹層次遍歷

2022-10-12 23:25:17

二叉樹父節(jié)點(diǎn)根節(jié)點(diǎn)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)