<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [LeetCode] 1361. Validate Binary Tree Nodes 驗證二叉樹


      You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.

      If node i has no left child then leftChild[i] will equal -1, similarly for the right child.

      Note that the nodes have no values and that we only use the node numbers in this problem.

      Example 1:

      Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
      Output: true

      Example 2:

      Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
      Output: false

      Example 3:

      Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
      Output: false

      Constraints:

      • n == leftChild.length == rightChild.length
      • 1 <= n <= 104
      • -1 <= leftChild[i], rightChild[i] <= n - 1

      這道題說是給了兩個數組 leftChild 和 rightChild,其中 leftChild[i] 表示結點i的左子結點,rightChild[i] 表示結點i的右子結點,若值為 -1,表示沒有左子結點或著右子結點,問給定的數組是否可以組成一個有效的二叉樹。對于二叉樹想必大家都不陌生,來看一下題目中給的例子,例子1是一棵有效的二叉樹,例子2中由于結點3有兩個父結點,所以不是有效的二叉樹,例子3中兩個結點互為父結點了,這也不是有效的二叉樹。二叉樹是一種特殊的有向圖,一棵有效的二叉樹至少要具備這個幾個特點,首先是只有一個根結點,即所有結點必須是連成一個整體的,其次是每個結點最多有兩個子結點,然后每個結點最多只有一個父結點,最后就是不能出現環。

      有向圖有個入度 In Degree 的概念,就是某個結點被其他結點連通的個數,對于有效二叉樹來說,除了根結點之外的每個結點的入度必須是1,即每個結點最多只有一個父結點,根結點的入度是0,其沒有父結點。那這里就可以通過計算每個結點的入度,來快速去除一些無效的二叉樹,這里用個長度為n的入度數組 inDegree,然后遍歷兩個數組,若左子結點不為 -1,則將其入度值自增1,此時判斷一下,若入度值大于1了,說明是無效的二叉樹,返回 false,對右子結點進行相同的處理。

      僅判斷結點的入度值是不夠的,比如例子3中,每個結點的入度都是1,但不是有效二叉樹,因為其沒有根結點,所以接下來需要找出根結點,而且只能有一個根結點,就是說只能有一個結點的入度是0,若找到了多個,則返回 false。找到了根結點后也還不能說就是有效的二叉函數了,還得保證所有的結點都是相連的,這個通過從根結點開始遍歷二叉樹,統計遍歷到的結點個數,若成功遍歷了n個結點,才能說是有效的二叉樹。遍歷的方法可以用 BFS 或者 DFS,這里先用 BFS,使用隊列 queue 來輔助運算,先把根結點 root 放進去,然后進行 while 循環,每次從 queue 中取出一個結點,計數器 cnt 自增1,然后看其左右子結點,若存在就加入到隊列中繼續循環,最后判斷 cnt 是否等于n即可,參見代碼如下:


      解法一:

      class Solution {
      public:
          bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
              int root = -1, cnt = 0;
              vector<int> inDegree(n);
              for (int i = 0; i < n; ++i) {
                  int left = leftChild[i], right = rightChild[i];
                  if (left != -1 && ++inDegree[left] > 1) return false;
                  if (right != -1 && ++inDegree[right] > 1) return false;
              }
              for (int i = 0; i < n; ++i) {
                  if (inDegree[i] != 0) continue;
                  if (root != -1) return false;
                  root = i;
              }
              if (root == -1) return false;
              queue<int> q{{root}};
              while (!q.empty()) {
                  auto t = q.front(); q.pop();
                  ++cnt;
                  if (leftChild[t] != -1) q.push(leftChild[t]);
                  if (rightChild[t] != -1) q.push(rightChild[t]);
              }
              return cnt == n;
          }
      };
      

      下面解法是用 DFS 來遍歷二叉樹,寫起來更加簡潔一些,在遞歸函數中首先判斷若當前結點 node 為 -1,說明不存在,則返回0,否則分別對其左右子結點調用遞歸函數,將返回值加起來,再加上1,就是以當前結點 node 為根結點的二叉樹的結點個數了,參見代碼如下:


      解法二:

      class Solution {
      public:
          bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
              int root = -1, cnt = 0;
              vector<int> inDegree(n);
              for (int i = 0; i < n; ++i) {
                  int left = leftChild[i], right = rightChild[i];
                  if (left != -1 && ++inDegree[left] > 1) return false;
                  if (right != -1 && ++inDegree[right] > 1) return false;
              }
              for (int i = 0; i < n; ++i) {
                  if (inDegree[i] != 0) continue;
                  if (root != -1) return false;
                  root = i;
              }
              if (root == -1) return false;
              return countNodes(leftChild, rightChild, root) == n;
          }
          int countNodes(vector<int>& leftChild, vector<int>& rightChild, int node) {
              if (node == -1) return 0;
              return 1 + countNodes(leftChild, rightChild, leftChild[node]) + countNodes(leftChild, rightChild, rightChild[node]);
          }
      };
      

      Github 同步地址:

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


      參考資料:

      https://leetcode.com/problems/validate-binary-tree-nodes/

      https://leetcode.com/problems/validate-binary-tree-nodes/solutions/517557/c-find-root-dfs/


      LeetCode All in One 題目講解匯總(持續更新中...)

      posted @ 2023-11-20 09:38  Grandyang  閱讀(226)  評論(0)    收藏  舉報
      Fork me on GitHub
      主站蜘蛛池模板: 国内自拍av在线免费| 四虎影视www在线播放| 欧美亚洲综合久久偷偷人人| 中文字幕日韩国产精品| 日韩精品一区二区三区激情| 久久精品国产亚洲夜色AV网站| 免费看成人aa片无码视频吃奶| 精品国产av一区二区果冻传媒| 福利视频一区二区在线| 国产一区二区亚洲精品| 国产无遮挡性视频免费看| 一区二区三区四区自拍偷拍| 免费av网站| 久久中文字幕无码专区| 国产精品一区二区黄色片| 深夜福利成人免费在线观看| 国产剧情福利一区二区麻豆 | 一区二区三区AV波多野结衣| 最近免费中文字幕大全| 亚洲成人精品在线伊人网| 亚洲精品中文字幕尤物综合| 丝袜美腿亚洲综合在线观看视频| 国产人妻大战黑人第1集| 国产精品国产三级国产a| 国产精品成人久久电影| 女子spa高潮呻吟抽搐| 精品国产免费一区二区三区香蕉 | 亚洲av成人一区二区三区| 免费久久人人爽人人爽AV| 久久亚洲中文无码咪咪爱| 九九热在线观看视频免费| 久久天天躁夜夜躁狠狠| 强奷漂亮人妻系列老师| 欧美日韩亚洲国产| 日韩精品av一区二区三区| 亚洲日韩久热中文字幕| 国内少妇人妻丰满av| 国产福利深夜在线播放| 高清国产亚洲精品自在久久| 国产精品视频全国免费观看| 亚洲av成人一区二区三区|