1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)298:二叉樹(shù)最長(zhǎng)連續(xù)序列

        共 4554字,需瀏覽 10分鐘

         ·

        2021-06-23 20:56

        算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問(wèn)題叫做 二叉樹(shù)最長(zhǎng)連續(xù)序列,我們先來(lái)看題面:
        https://leetcode-cn.com/problems/binary-tree-longest-consecutive-sequence/

        Given a binary tree, find the length of the longest consecutive sequence path.

        The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections .

        The longest consecutive path need to be from parent to child (cannot be the reverse).


        給你一棵指定的二叉樹(shù),請(qǐng)你計(jì)算它最長(zhǎng)連續(xù)序列路徑的長(zhǎng)度。
        該路徑,可以是從某個(gè)初始結(jié)點(diǎn)到樹(shù)中任意結(jié)點(diǎn),通過(guò)「父 - 子」關(guān)系連接而產(chǎn)生的任意路徑。
        這個(gè)最長(zhǎng)連續(xù)的路徑,必須從父結(jié)點(diǎn)到子結(jié)點(diǎn),反過(guò)來(lái)是不可以的。

        示例



        解題


        找出左右結(jié)點(diǎn)中可能的更長(zhǎng)的路徑,進(jìn)行保存


        /**
         * Definition for a binary tree node.
         * struct TreeNode {
         * int val;
         * TreeNode *left;
         * TreeNode *right;
         * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
         * };
         */

        class Solution {
        public:
            void helper(TreeNode* root,int count,int& max_count){
                if(root==NULL){//終止條件
                    return;
                }
                //若左結(jié)點(diǎn)不為空,則可以接著遍歷
                if(root->left){
                  //若左結(jié)點(diǎn)和根節(jié)點(diǎn)的值的關(guān)系滿足連續(xù),則調(diào)整長(zhǎng)度
                    if(root->val+1==root->left->val){
                        max_count=max(max_count,count+1);
                        helper(root->left,count+1,max_count);
                    }
                    else{
                      //否則將長(zhǎng)度重新置為1調(diào)整
                        helper(root->left,1,max_count);
                    }
                }
                //若右結(jié)點(diǎn)和根節(jié)點(diǎn)的值的關(guān)系滿足連續(xù),則調(diào)整長(zhǎng)度
                if(root->right){
                    if(root->val+1==root->right->val){
                        max_count=max(max_count,count+1);
                        helper(root->right,count+1,max_count);
                    }
                    else{
                      //否則將長(zhǎng)度重新置為1進(jìn)行統(tǒng)計(jì)
                        helper(root->right,1,max_count);
                    }
                }
            }
            int longestConsecutive(TreeNode* root) {
                if(root==NULL){
                    return 0;
                }
                int max_count=1;//初始長(zhǎng)度
                helper(root,1,max_count);
                return max_count;
            }
        };


        好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

        上期推文:
        LeetCode1-280題匯總,希望對(duì)你有點(diǎn)幫助!
        LeetCode刷題實(shí)戰(zhàn)281:鋸齒迭代器
        LeetCode刷題實(shí)戰(zhàn)282:給表達(dá)式添加運(yùn)算符
        LeetCode刷題實(shí)戰(zhàn)283:移動(dòng)零
        LeetCode刷題實(shí)戰(zhàn)284:頂端迭代器
        LeetCode刷題實(shí)戰(zhàn)285:二叉搜索樹(shù)中的順序后繼
        LeetCode刷題實(shí)戰(zhàn)286:墻和門
        LeetCode刷題實(shí)戰(zhàn)287:尋找重復(fù)數(shù)
        LeetCode刷題實(shí)戰(zhàn)288:?jiǎn)卧~的唯一縮寫
        LeetCode刷題實(shí)戰(zhàn)289:生命游戲
        LeetCode刷題實(shí)戰(zhàn)290:?jiǎn)卧~規(guī)律
        LeetCode刷題實(shí)戰(zhàn)291:?jiǎn)卧~規(guī)律II
        LeetCode刷題實(shí)戰(zhàn)292:Nim 游戲
        LeetCode刷題實(shí)戰(zhàn)293:翻轉(zhuǎn)游戲
        LeetCode刷題實(shí)戰(zhàn)294:翻轉(zhuǎn)游戲II
        LeetCode刷題實(shí)戰(zhàn)295:數(shù)據(jù)流的中位數(shù)
        LeetCode刷題實(shí)戰(zhàn)296:最佳的碰頭地點(diǎn)
        LeetCode刷題實(shí)戰(zhàn)297:二叉樹(shù)的序列化與反序列化

        瀏覽 57
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久久久久久久成人电影 | 久久男女视频 | 东京热无码不卡视频 | 大桥未久av一区二区三区中文 | 国产91清纯白嫩初高中漫画 | 无码激情做a爰片毛片A片孕妇 | 曰韩无码| 欧美操逼免费网站 | 91视频porny | 成人H无里番在线观看视频精品 |