成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

leetcode113. Path Sum II

Eirunye / 1186人閱讀

摘要:題目要求從樹中找到所有符合條件的從根節(jié)點(diǎn)到葉節(jié)點(diǎn)路徑,條件即為樹上所有節(jié)點(diǎn)值的和等于目標(biāo)值??梢詤⒖歼@篇博客思路和代碼其實(shí)這里本質(zhì)上的思路并沒有改變,還是采用深度優(yōu)先算法,采用自頂向下遞歸的方式將符合條件的結(jié)果放入結(jié)果集中。

題目要求
Given a binary tree and a sum, find all root-to-leaf paths where each path"s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / 
            4   8
           /   / 
          11  13  4
         /      / 
        7    2  5   1
return
[
   [5,4,11,2],
   [5,8,4,5]
]

從樹中找到所有符合條件的從根節(jié)點(diǎn)到葉節(jié)點(diǎn)路徑,條件即為樹上所有節(jié)點(diǎn)值的和等于目標(biāo)值。
Path Sum I可以參考這篇博客

思路和代碼

其實(shí)這里本質(zhì)上的思路并沒有改變,還是采用深度優(yōu)先算法,采用自頂向下遞歸的方式將符合條件的結(jié)果放入結(jié)果集中。

    public List> pathSum(TreeNode root, int sum) {
        List> result = new ArrayList>();
        pathSum(root, sum, new ArrayList(), result);
        return result;
    }
    
    public void pathSum(TreeNode root, int sum, List path, List> result){
        if(root==null) return;
        sum -= root.val;
        if(isLeaf(root) && sum==0){
            path.add(root.val);
            result.add(new ArrayList(path));
            path.remove(path.size()-1);
            return;
        }
        path.add(root.val);
        pathSum(root.left, sum, path, result);
        pathSum(root.right, sum, path, result);
        path.remove(path.size()-1);
        
        
    }
    
    private boolean isLeaf(TreeNode node){
        return node!=null && node.left==null && node.right==null;
    }


想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會不定期的發(fā)放福利哦~

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/67384.html

相關(guān)文章

  • [LeetCode] Path Sum (I & II & III)

    摘要: 112. Path Sum Problem Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node...

    張金寶 評論0 收藏0
  • [Leetcode-Tree] Path Sum I II III

    摘要:解題思路利用遞歸,對于每個(gè)根節(jié)點(diǎn),只要左子樹和右子樹中有一個(gè)滿足,就返回每次訪問一個(gè)節(jié)點(diǎn),就將該節(jié)點(diǎn)的作為新的進(jìn)行下一層的判斷。代碼解題思路本題的不同點(diǎn)是可以不從開始,不到結(jié)束。代碼當(dāng)前節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)左節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)右節(jié)點(diǎn)開始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...

    notebin 評論0 收藏0
  • [Leetcode] Path Sum I & II & III 路徑和1,2,3

    摘要:只要我們能夠有一個(gè)以某一中間路徑和為的哈希表,就可以隨時(shí)判斷某一節(jié)點(diǎn)能否和之前路徑相加成為目標(biāo)值。 最新更新請見:https://yanjia.me/zh/2019/01/... Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addin...

    caiyongji 評論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡單難度攻略已經(jīng)到題,所以后面會調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...

    tain335 評論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...

    張漢慶 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<