<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>in-degree Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/in-degree/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/in-degree/</link>
	<description></description>
	<lastBuildDate>Sat, 22 Aug 2020 18:31:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>in-degree Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/in-degree/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1557. Minimum Number of Vertices to Reach All Nodes</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 22 Aug 2020 18:31:08 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[in-degree]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7275</guid>

					<description><![CDATA[<p>Given a&#160;directed acyclic graph,&#160;with&#160;n&#160;vertices numbered from&#160;0&#160;to&#160;n-1,&#160;and an array&#160;edges&#160;where&#160;edges[i] = [fromi, toi]&#160;represents a directed edge from node&#160;fromi&#160;to node&#160;toi. Find&#160;the smallest set of vertices from which all&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes/">花花酱 LeetCode 1557. Minimum Number of Vertices to Reach All Nodes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a<strong>&nbsp;directed acyclic graph</strong>,&nbsp;with&nbsp;<code>n</code>&nbsp;vertices numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>,&nbsp;and an array&nbsp;<code>edges</code>&nbsp;where&nbsp;<code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code>&nbsp;represents a directed edge from node&nbsp;<code>from<sub>i</sub></code>&nbsp;to node&nbsp;<code>to<sub>i</sub></code>.</p>



<p>Find&nbsp;<em>the smallest set of vertices from which all nodes in the graph are reachable</em>. It&#8217;s guaranteed that a unique solution exists.</p>



<p>Notice that you can return the vertices in any order.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/07/07/untitled22.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
<strong>Output:</strong> [0,3]
<strong>Explanation: </strong>It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/07/07/untitled.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
<strong>Output:</strong> [0,2,3]
<strong>Explanation: </strong>Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>2 &lt;= n &lt;= 10^5</code></li><li><code>1 &lt;= edges.length &lt;= min(10^5, n * (n - 1) / 2)</code></li><li><code>edges[i].length == 2</code></li><li><code>0 &lt;= from<sub>i,</sub>&nbsp;to<sub>i</sub>&nbsp;&lt; n</code></li><li>All pairs&nbsp;<code>(from<sub>i</sub>, to<sub>i</sub>)</code>&nbsp;are distinct.</li></ul>



<h2><strong>Solution: In degree</strong></h2>



<p>Nodes with 0 in degree will be the answer.<br>Time complexity: O(E+V)<br>Space complexity: O(V)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; findSmallestSetOfVertices(int n, vector&lt;vector&lt;int&gt;&gt;&amp; edges) {
    vector&lt;int&gt; in(n);    
    for (const auto&amp; e : edges) ++in[e[1]];
    vector&lt;int&gt; ans;
    for (int i = 0; i &lt; n; ++i)
      if (in[i] == 0) ans.push_back(i);
    return ans;    
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes/">花花酱 LeetCode 1557. Minimum Number of Vertices to Reach All Nodes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1361. Validate Binary Tree Nodes</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1361-validate-binary-tree-nodes/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1361-validate-binary-tree-nodes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Feb 2020 10:04:11 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[in-degree]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6370</guid>

					<description><![CDATA[<p>You have&#160;n&#160;binary tree nodes&#160;numbered from&#160;0&#160;to&#160;n - 1&#160;where node&#160;i&#160;has two children&#160;leftChild[i]&#160;and&#160;rightChild[i], return&#160;true&#160;if and only if&#160;all&#160;the given nodes form&#160;exactly one&#160;valid binary tree. If node&#160;i&#160;has no left child&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1361-validate-binary-tree-nodes/">花花酱 LeetCode 1361. Validate Binary Tree Nodes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<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>



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



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
<strong>Output:</strong> true
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
<strong>Output:</strong> false
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, leftChild = [1,0], rightChild = [-1,-1]
<strong>Output:</strong> false
</pre>



<p><strong>Example 4:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex4.png" alt=""/></figure>



<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]
<strong>Output:</strong> false
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Count in-degrees for each node</strong></h2>



<p>in degree must &lt;= 1 and there must be exact one node that has 0 in-degree.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool validateBinaryTreeNodes(int n, vector&lt;int&gt;&amp; leftChild, vector&lt;int&gt;&amp; rightChild) {
    vector&lt;int&gt; in(n);
    for (int l : leftChild) if (l &gt;= 0) ++in[l];
    for (int r : rightChild) if (r &gt;= 0) ++in[r];
    int zero = 0;
    for (int i = 0; i &lt; n; ++i) {
      if (in[i] &gt; 1) return false;
      if (in[i] == 0) ++zero;
    }
    // # of nodes without parent must be 1.
    return zero == 1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1361-validate-binary-tree-nodes/">花花酱 LeetCode 1361. Validate Binary Tree Nodes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-1361-validate-binary-tree-nodes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
