<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)572:另一棵樹的子樹

          共 2287字,需瀏覽 5分鐘

           ·

          2022-04-11 11:55

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

          今天和大家聊的問題叫做?另一棵樹的子樹,我們先來看題面:
          https://leetcode-cn.com/problems/subtree-of-another-tree/

          Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.

          A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.

          給你兩棵二叉樹 root 和 subRoot 。檢驗(yàn) root 中是否包含和 subRoot 具有相同結(jié)構(gòu)和節(jié)點(diǎn)值的子樹。如果存在,返回 true ;否則,返回 false 。

          二叉樹 tree 的一棵子樹包括 tree 的某個(gè)節(jié)點(diǎn)和這個(gè)節(jié)點(diǎn)的所有后代節(jié)點(diǎn)。tree 也可以看做它自身的一棵子樹。

          示例




          解題


          這道題就是在 root 的每個(gè)子節(jié)點(diǎn)上,判斷由該子節(jié)點(diǎn)構(gòu)成的子樹是否和 subRoot 這顆樹相等。判斷兩顆樹相等需要同時(shí)滿足三個(gè)條件:當(dāng)前兩顆樹的根節(jié)點(diǎn)值相等;兩顆樹的左子樹相等;兩棵樹的右子樹相等。而判斷 一棵樹 是否為 另一顆樹 的子樹只需滿足以下條件中的一個(gè):當(dāng)前兩棵樹相等;或 一棵樹 是 另一顆樹 的左子樹;或 一棵樹 是 另一棵樹 的右子樹。此題采用遞歸法求解。

          class?Solution?{
          public:
          ????bool isSubtree(TreeNode* root, TreeNode* subRoot) {
          ????????if(root==NULL||subRoot==NULL)
          ????????????return?false;
          ????????if(root->val!=subRoot->val){
          ????????????return?isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);
          ????????}else{
          ????????????// return judge(root,subRoot);
          ????????????return?judge(root,subRoot)||isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot); // 這是為了應(yīng)對(duì)出現(xiàn)有重復(fù)元素出現(xiàn)的情況,比如root: 1 1, subroot:1
          ????????}
          ????????return?false;
          ????}
          ????bool judge(TreeNode* root1, TreeNode* root2){
          ????????// 這是用來判斷子樹的
          ????????if(root1==NULL&&root2==NULL) // 兩個(gè)都為空,說明比對(duì)完了,返回true
          ????????????return?true;
          ????????if(root1==NULL||root2==NULL) // 其中一個(gè)為空,說明一個(gè)比完,一個(gè)還有,返回false
          ????????????return?false;
          ????????// 如果是判斷子結(jié)構(gòu)的就用下面這種方式
          ????????// if(root2==NULL)
          ????????// return true;
          ????????// if(root1==NULL) // 此時(shí)肯定root2不為null,root1為null,所以false;
          ????????// return false;
          ????????if(root1->val!=root2->val)
          ????????????return?false;
          ????????else?
          ????????????return?judge(root1->left,root2->left)&&judge(root1->right, root2->right);
          ????}
          };


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

          上期推文:

          LeetCode1-560題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)561:數(shù)組拆分 I
          LeetCode刷題實(shí)戰(zhàn)562:矩陣中最長的連續(xù)1線段
          LeetCode刷題實(shí)戰(zhàn)563:二叉樹的坡度
          LeetCode刷題實(shí)戰(zhàn)564:尋找最近的回文數(shù)
          LeetCode刷題實(shí)戰(zhàn)565:數(shù)組嵌套
          LeetCode刷題實(shí)戰(zhàn)566:重塑矩陣
          LeetCode刷題實(shí)戰(zhàn)567:字符串的排列
          LeetCode刷題實(shí)戰(zhàn)568:最大休假天數(shù)
          LeetCode刷題實(shí)戰(zhàn)569:?jiǎn)T工薪水中位數(shù)
          LeetCode刷題實(shí)戰(zhàn)570:至少有5名直接下屬的經(jīng)理
          LeetCode刷題實(shí)戰(zhàn)571:給定數(shù)字的頻率查詢中位數(shù)

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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  麻豆精品在线影院 | 国产激情自拍 | 黄色操逼片黄色操逼 | 思思热在线 | 四虎8848精品成人免费网站 |