c++路徑之和實(shí)例分析

本文小編為大家詳細(xì)介紹“c++路徑之和實(shí)例分析”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“c++路徑之和實(shí)例分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、融安網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、融安網(wǎng)絡(luò)營(yíng)銷、融安企業(yè)策劃、融安品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供融安建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:muchs.cn

算法:

算法采用遞歸,核心在于如何找到遞歸的終止條件,具體步驟如下:

1.采用遞歸的方式,sum的數(shù)值要隨著遍歷過(guò)的節(jié)點(diǎn)做遞減操作,sum = sum-root.Val2.遞歸的終止條件sum==0是其中之一,如果要求是葉子節(jié)點(diǎn)的也需要加上

題目1:路徑總和

代碼實(shí)現(xiàn):

/** * Definition for a binary tree node. * type TreeNode struct { *     Val int *     Left *TreeNode *     Right *TreeNode * } */func hasPathSum(root *TreeNode, sum int) bool {    if root == nil {         return false    }    // 葉子節(jié)點(diǎn)的判斷,排除非葉子節(jié)點(diǎn)==sum的情況    if root.Left == nil && root.Right == nil {        return sum == root.Val    }    res := sum - root.Val     if hasPathSum(root.Left,res) {        return true    }    if hasPathSum(root.Right,res) {        return true    }    return false}

題目2:路徑總和2

代碼實(shí)現(xiàn):

/** * Definition for a binary tree node. * type TreeNode struct { *     Val int *     Left *TreeNode *     Right *TreeNode * } */var res [][]intfunc pathSum(root *TreeNode, sum int) [][]int {    res = [][]int{} // 為了清空 res 上次的數(shù)值    if root == nil {         return nil    }    // var res [][]int     var tmp []int    dfs(root,sum,tmp)    return res}func dfs(root *TreeNode, sum int, tmp []int) {    if root == nil {         return     }    tmp = append(tmp,root.Val)    if sum == root.Val && root.Left == nil && root.Right == nil {        r := make([]int, len(tmp)) // 防止tmp對(duì)應(yīng)的共享內(nèi)容被修改        copy(r, tmp)        res = append(res, r)        return     }       dfs(root.Left,sum-root.Val,tmp)    dfs(root.Right,sum-root.Val,tmp)       return }

題目3:路徑總和3

代碼實(shí)現(xiàn):

/** * Definition for a binary tree node. * type TreeNode struct { *     Val int *     Left *TreeNode *     Right *TreeNode * } */func pathSum(root *TreeNode, sum int) int {    if root == nil {        return 0    }    result := countPath(root,sum)    result += pathSum(root.Left,sum)    result += pathSum(root.Right,sum)    return result}func countPath(root *TreeNode, sum int) int {    if root == nil {         return 0    }    count := 0    res := sum - root.Val    if res == 0 {        count = 1    }    return count + countPath(root.Left,res) + countPath(root.Right,res)}/*以當(dāng)前節(jié)點(diǎn)作為頭結(jié)點(diǎn)的路徑數(shù)量以當(dāng)前節(jié)點(diǎn)的左孩子作為頭結(jié)點(diǎn)的路徑數(shù)量以當(dāng)前節(jié)點(diǎn)的右孩子作為頭結(jié)點(diǎn)的路徑數(shù)量*/

讀到這里,這篇“c++路徑之和實(shí)例分析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文名稱:c++路徑之和實(shí)例分析
轉(zhuǎn)載注明:http://muchs.cn/article10/ghegdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、域名注冊(cè)、網(wǎng)站維護(hù)、網(wǎng)站排名、微信公眾號(hào)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)