<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)617:合并二叉樹

          共 2747字,需瀏覽 6分鐘

           ·

          2022-05-27 16:12

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

          今天和大家聊的問(wèn)題叫做?合并二叉樹,我們先來(lái)看題面:
          https://leetcode.cn/problems/merge-two-binary-trees/
          You are given two binary trees root1 and root2.

          Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

          Return the merged tree.

          Note: The merging process must start from the root nodes of both trees.

          給你兩棵二叉樹:root1 和 root2 。

          想象一下,當(dāng)你將其中一棵覆蓋到另一棵之上時(shí),兩棵樹上的一些節(jié)點(diǎn)將會(huì)重疊(而另一些不會(huì))。你需要將這兩棵樹合并成一棵新二叉樹。合并的規(guī)則是:如果兩個(gè)節(jié)點(diǎn)重疊,那么將這兩個(gè)節(jié)點(diǎn)的值相加作為合并后節(jié)點(diǎn)的新值;否則,不為 null 的節(jié)點(diǎn)將直接作為新二叉樹的節(jié)點(diǎn)。

          返回合并后的二叉樹。

          注意: 合并過(guò)程必須從兩個(gè)樹的根節(jié)點(diǎn)開始。

          示例


          解題

          https://blog.csdn.net/Changersh/article/details/123969046

          主要思路:迭代,層序

          套層序模板,只不過(guò)入隊(duì) / 出隊(duì)的時(shí)候變成了兩棵樹的結(jié)點(diǎn),
          最開始的時(shí)候還是要判斷一下,兩個(gè)根節(jié)點(diǎn)是否是空的

          while循環(huán)里面,同時(shí)出隊(duì)兩棵樹的對(duì)應(yīng)結(jié)點(diǎn),
          開始的時(shí)候直接將root2 的val加到root1上面即可

          之后分以下幾種情況

          root1和root2左節(jié)點(diǎn)都不空,就將他們分別入隊(duì)
          root1和root2右節(jié)點(diǎn)都不空,分別入隊(duì)
          root1左節(jié)點(diǎn)為空,而root2左節(jié)點(diǎn)不空,直接把root2左節(jié)點(diǎn)賦值給root1
          root1右節(jié)點(diǎn)為空,而root2右節(jié)點(diǎn)不空,直接把root2右節(jié)點(diǎn)賦值給root1
          (不需要考慮root1不空但是root2 空的結(jié)點(diǎn),因?yàn)閞oot1是母樹,直接接在自身就好了)

          class?Solution?{
          public:
          ????TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
          ????????if?(root1 == NULL) return?root2;
          ????????if?(root2 == NULL) return?root1;

          ????????queue que;
          ????????// 此時(shí)兩個(gè)根節(jié)點(diǎn)一定不空
          ????????que.push(root1);
          ????????que.push(root2);

          ????????while?(!que.empty()) {
          ????????????TreeNode* node1 = que.front();
          ????????????que.pop();

          ????????????TreeNode* node2 = que.front();
          ????????????que.pop();

          ????????????node1->val += node2->val;

          ????????????// 如果兩個(gè)左子樹都不空,入隊(duì)
          ????????????if?(node1->left && node2->left) {
          ????????????????que.push(node1->left);
          ????????????????que.push(node2->left);
          ????????????}

          ????????????// 右子樹都不空,入隊(duì)
          ????????????if?(node1->right && node2->right) {
          ????????????????que.push(node1->right);
          ????????????????que.push(node2->right);
          ????????????}

          ????????????// 因?yàn)槭前裷oot1 當(dāng)作母樹,所以root1 結(jié)點(diǎn)是空的時(shí)候不用再入隊(duì)了,因?yàn)橹苯影裷oot2 接到它下面了
          ????????????if?(node1->left == NULL?&& node2->left) {
          ????????????????node1->left = node2->left;
          ????????????}

          ????????????if?(node1->right == NULL?&& node2->right) {
          ????????????????node1->right = node2->right;
          ????????????}

          ????????}

          ????????return?root1;
          ????}
          };


          上期推文:
          LeetCode1-600題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)601:體育館的人流量
          LeetCode刷題實(shí)戰(zhàn)602:好友申請(qǐng) II :誰(shuí)有最多的好友
          LeetCode刷題實(shí)戰(zhàn)603:連續(xù)空余座位
          LeetCode刷題實(shí)戰(zhàn)604:迭代壓縮字符串
          LeetCode刷題實(shí)戰(zhàn)605:種花問(wèn)題
          LeetCode刷題實(shí)戰(zhàn)606:根據(jù)二叉樹創(chuàng)建字符串
          LeetCode刷題實(shí)戰(zhàn)607:銷售員
          LeetCode刷題實(shí)戰(zhàn)608:樹節(jié)點(diǎn)
          LeetCode刷題實(shí)戰(zhàn)609:在系統(tǒng)中查找重復(fù)文件
          LeetCode刷題實(shí)戰(zhàn)610:判斷三角形
          LeetCode刷題實(shí)戰(zhàn)611:有效三角形的個(gè)數(shù)
          LeetCode刷題實(shí)戰(zhàn)612:平面上的最近距離
          LeetCode刷題實(shí)戰(zhàn)613:直線上的最近距離
          LeetCode刷題實(shí)戰(zhàn)614:二級(jí)關(guān)注者
          LeetCode刷題實(shí)戰(zhàn)615:平均工資:部門與公司比較
          LeetCode刷題實(shí)戰(zhàn)616:給字符串添加加粗標(biāo)簽

          瀏覽 27
          點(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>
                  那个网站可观看毛片 | 欧美熟妇激情一区二区三区 | 青青草a 人人操人 | 亚洲日韩在线观看网站 | www.色婷婷五月综合在线色吧 |