欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode(129.求根到葉節(jié)點(diǎn)數(shù)字之和)

 更新時(shí)間:2021年07月27日 15:10:17   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(129.求根到葉節(jié)點(diǎn)數(shù)字之和),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[LeetCode] 129. Sum Root to Leaf Numbers 求根到葉節(jié)點(diǎn)數(shù)字之和

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

Note: A leaf is a node with no children.

Example:

Input: [1,2,3]
1
/ \
2   3
Output: 25
Explanation:
The root-to-leaf path

1->2

represents the number

12

.
The root-to-leaf path

1->3

represents the number

13

.
Therefore, sum = 12 + 13 =

25

.

Example 2:

Input: [4,9,0,5,1]
4
/ \
9   0
/ \
5   1
Output: 1026
Explanation:
The root-to-leaf path

4->9->5

represents the number 495.
The root-to-leaf path

4->9->1

represents the number 491.
The root-to-leaf path

4->0

represents the number 40.
Therefore, sum = 495 + 491 + 40 =

1026

.

這道求根到葉節(jié)點(diǎn)數(shù)字之和的題跟之前的求 Path Sum 很類似,都是利用DFS遞歸來解,這道題由于不是單純的把各個(gè)節(jié)點(diǎn)的數(shù)字相加,而是每遇到一個(gè)新的子結(jié)點(diǎn)的數(shù)字,要把父結(jié)點(diǎn)的數(shù)字?jǐn)U大10倍之后再相加。如果遍歷到葉結(jié)點(diǎn)了,就將當(dāng)前的累加結(jié)果sum返回。如果不是,則對其左右子結(jié)點(diǎn)分別調(diào)用遞歸函數(shù),將兩個(gè)結(jié)果相加返回即可,參見代碼如下:

解法一:

class Solution {
public:
    int sumNumbers(TreeNode* root) {
        return sumNumbersDFS(root, 0);
    }
    int sumNumbersDFS(TreeNode* root, int sum) {
        if (!root) return 0;
        sum = sum * 10 + root->val;
        if (!root->left && !root->right) return sum;
        return sumNumbersDFS(root->left, sum) + sumNumbersDFS(root->right, sum);
    }
};

我們也可以采用迭代的寫法,這里用的是先序遍歷的迭代寫法,使用棧來輔助遍歷,首先將根結(jié)點(diǎn)壓入棧,然后進(jìn)行while循環(huán),取出棧頂元素,如果是葉結(jié)點(diǎn),那么將其值加入結(jié)果res。如果其右子結(jié)點(diǎn)存在,那么其結(jié)點(diǎn)值加上當(dāng)前結(jié)點(diǎn)值的10倍,再將右子結(jié)點(diǎn)壓入棧。同理,若左子結(jié)點(diǎn)存在,那么其結(jié)點(diǎn)值加上當(dāng)前結(jié)點(diǎn)值的10倍,再將左子結(jié)點(diǎn)壓入棧,是不是跟之前的 Path Sum 極其類似呢,參見代碼如下:

解法二:

class Solution {
public:
    int sumNumbers(TreeNode* root) {
        if (!root) return 0;
        int res = 0;
        stack<TreeNode*> st{{root}};
        while (!st.empty()) {
            TreeNode *t = st.top(); st.pop();
            if (!t->left && !t->right) {
                res += t->val;
            }
            if (t->right) {
                t->right->val += t->val * 10;
                st.push(t->right);
            }
            if (t->left) {
                t->left->val += t->val * 10;
                st.push(t->left);
            }
        }
        return res;
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/129

類似題目:

Path Sum

Binary Tree Maximum Path Sum

參考資料:

https://leetcode.com/problems/sum-root-to-leaf-numbers/

https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/41367/Non-recursive-preorder-traverse-Java-solution

https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/41452/Iterative-C%2B%2B-solution-using-stack-(similar-to-postorder-traversal)

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(129.求根到葉節(jié)點(diǎn)數(shù)字之和)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)求根到葉節(jié)點(diǎn)數(shù)字之和內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++中的函數(shù)匯總

    C++中的函數(shù)匯總

    這篇文章主要介紹了 C++中的函數(shù)匯總的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • C語言實(shí)現(xiàn)線索二叉樹的定義與遍歷示例

    C語言實(shí)現(xiàn)線索二叉樹的定義與遍歷示例

    這篇文章主要介紹了C語言實(shí)現(xiàn)線索二叉樹的定義與遍歷,結(jié)合具體實(shí)例形式分析了基于C語言的線索二叉樹定義及遍歷操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • C語言結(jié)構(gòu)及隊(duì)列實(shí)現(xiàn)示例詳解

    C語言結(jié)構(gòu)及隊(duì)列實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了C語言實(shí)現(xiàn)隊(duì)列示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • C++關(guān)于引用(reference)的代碼案例

    C++關(guān)于引用(reference)的代碼案例

    引用是C++中的一種重要特性,它可以讓代碼更加高效、簡潔和易讀,本文將深入探討引用的相關(guān)知識,包括引用的概念、使用方法、優(yōu)點(diǎn)和注意事項(xiàng)等。建議根據(jù)給出的代碼案例練一下,熟悉即可
    2023-05-05
  • 打印菱形以及斐波納契數(shù)列的幾種解法介紹

    打印菱形以及斐波納契數(shù)列的幾種解法介紹

    本篇文章是對打印菱形及斐波納契數(shù)列的幾種解法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C++11 std::function和std::bind 的使用示例詳解

    C++11 std::function和std::bind 的使用示例詳解

    C++11中的std::function和std::bind是函數(shù)對象的重要組成部分,它們可以用于將函數(shù)和參數(shù)綁定在一起,形成一個(gè)可調(diào)用的對象,這篇文章主要介紹了C++11 std::function和std::bind 的使用示例詳解,需要的朋友可以參考下
    2023-03-03
  • C語言指針詳解及用法示例

    C語言指針詳解及用法示例

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價(jià)值。需要的朋友可以了解下。
    2017-11-11
  • C++ 數(shù)據(jù)結(jié)構(gòu)之布隆過濾器

    C++ 數(shù)據(jù)結(jié)構(gòu)之布隆過濾器

    這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)之布隆過濾器的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 關(guān)于函數(shù)傳參問題(指針傳參,值傳參,引用傳參)

    關(guān)于函數(shù)傳參問題(指針傳參,值傳參,引用傳參)

    這篇文章主要介紹了關(guān)于函數(shù)傳參問題(指針傳參,值傳參,引用傳參),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • C及C++?基礎(chǔ)循環(huán)示例詳解

    C及C++?基礎(chǔ)循環(huán)示例詳解

    這篇文章主要介紹了C及C++?中的循環(huán)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評論