{"id":6370,"date":"2020-02-23T02:04:11","date_gmt":"2020-02-23T10:04:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6370"},"modified":"2020-02-23T02:07:23","modified_gmt":"2020-02-23T10:07:23","slug":"leetcode-1361-validate-binary-tree-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1361-validate-binary-tree-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1361. Validate Binary Tree Nodes"},"content":{"rendered":"\n<p>You have&nbsp;<code>n<\/code>&nbsp;binary tree nodes&nbsp;numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;where node&nbsp;<code>i<\/code>&nbsp;has two children&nbsp;<code>leftChild[i]<\/code>&nbsp;and&nbsp;<code>rightChild[i]<\/code>, return&nbsp;<code>true<\/code>&nbsp;if and only if&nbsp;<strong>all<\/strong>&nbsp;the given nodes form&nbsp;<strong>exactly one<\/strong>&nbsp;valid binary tree.<\/p>\n\n\n\n<p>If node&nbsp;<code>i<\/code>&nbsp;has no left child then&nbsp;<code>leftChild[i]<\/code>&nbsp;will equal&nbsp;<code>-1<\/code>, similarly for the right child.<\/p>\n\n\n\n<p>Note that the nodes have no values and that we only use the node numbers in this problem.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/08\/23\/1503_ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/08\/23\/1503_ex2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/08\/23\/1503_ex3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, leftChild = [1,0], rightChild = [-1,-1]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/08\/23\/1503_ex4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^4<\/code><\/li><li><code>leftChild.length == rightChild.length == n<\/code><\/li><li><code>-1 &lt;= leftChild[i], rightChild[i] &lt;= n - 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Count in-degrees for each node<\/strong><\/h2>\n\n\n\n<p>in degree must &lt;= 1 and there must be exact one node that has 0 in-degree.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n    vector<int> in(n);\n    for (int l : leftChild) if (l >= 0) ++in[l];\n    for (int r : rightChild) if (r >= 0) ++in[r];\n    int zero = 0;\n    for (int i = 0; i < n; ++i) {\n      if (in[i] > 1) return false;\n      if (in[i] == 0) ++zero;\n    }\n    \/\/ # of nodes without parent must be 1.\n    return zero == 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have&nbsp;n&nbsp;binary tree nodes&nbsp;numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;where node&nbsp;i&nbsp;has two children&nbsp;leftChild[i]&nbsp;and&nbsp;rightChild[i], return&nbsp;true&nbsp;if and only if&nbsp;all&nbsp;the given nodes form&nbsp;exactly one&nbsp;valid binary tree. If node&nbsp;i&nbsp;has no left child&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[30,560,28],"class_list":["post-6370","post","type-post","status-publish","format-standard","hentry","category-tree","tag-binary-tree","tag-in-degree","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6370"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6370\/revisions"}],"predecessor-version":[{"id":6372,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6370\/revisions\/6372"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}