<?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>depth Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/depth/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/depth/</link>
	<description></description>
	<lastBuildDate>Sat, 15 Dec 2018 16:41:07 +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>depth Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/depth/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 111. Minimum Depth of Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-111-minimum-depth-of-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-111-minimum-depth-of-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 15 Dec 2018 16:34:32 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[depth]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4442</guid>

					<description><![CDATA[<p>Problem Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-111-minimum-depth-of-binary-tree/">花花酱 LeetCode 111. Minimum Depth of Binary Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given a binary tree, find its minimum depth.</p>
<p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p>
<p><strong>Note:</strong> A leaf is a node with no children.</p>
<p><strong>Example:</strong></p>
<p>Given binary tree <code>[3,9,20,null,null,15,7]</code>,</p>
<pre class="crayon:false">    3
   / \
  9  20
    /  \
   15   7</pre>
<p>return its minimum depth = 2.</p>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 4 ms
class Solution {
public:
  int minDepth(TreeNode* root) {
    if (!root) return 0;
    if (!root-&gt;left &amp;&amp; !root-&gt;right) return 1;
    int l = root-&gt;left ? minDepth(root-&gt;left) : INT_MAX;
    int r = root-&gt;right ? minDepth(root-&gt;right) : INT_MAX;
    return min(l, r) + 1;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">class Solution:
  def minDepth(self, root):
    if not root: return 0
    if not root.left and not root.right: return 1
    l = self.minDepth(root.left)
    r = self.minDepth(root.right)
    if not root.left: return 1 + r
    if not root.right: return 1 + l
    return 1 + min(l, r)</pre><p></div></div></p>
<h1><strong>Related Problem</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-104-maximum-depth-of-binary-tree/">花花酱 LeetCode 104. Maximum Depth of Binary Tree</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-111-minimum-depth-of-binary-tree/">花花酱 LeetCode 111. Minimum Depth of Binary Tree</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-111-minimum-depth-of-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 559. Maximum Depth of N-ary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-559-maximum-depth-of-n-ary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-559-maximum-depth-of-n-ary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Jul 2018 14:31:30 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[depth]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[n-ary]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3122</guid>

					<description><![CDATA[<p>Problem Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-559-maximum-depth-of-n-ary-tree/">花花酱 LeetCode 559. Maximum Depth of N-ary Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem</h1>
<p>Given a n-ary tree, find its maximum depth.</p>
<p>The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>
<p>For example, given a <code>3-ary</code> tree:</p>
<p>&nbsp;</p>
<p><img src="https://leetcode.com/static/images/problemset/NaryTreeExample.png" width="40%" height="40%" /></p>
<p>&nbsp;</p>
<p>We should return its max depth, which is 3.</p>
<p><b>Note:</b></p>
<ol>
<li>The depth of the tree is at most <code>1000</code>.</li>
<li>The total number of nodes is at most <code>5000</code>.</li>
</ol>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 40 ms
class Solution {
public:
  int maxDepth(Node* root) {
    if (root == nullptr) return 0;
    int depth = 0;
    for (auto child : root-&gt;children)
      depth = max(depth, maxDepth(child));
    return depth + 1;
  }
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-590-n-ary-tree-postorder-traversal/">花花酱 LeetCode 590. N-ary Tree Postorder Traversal</a></li>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-589-n-ary-tree-preorder-traversal/">花花酱 LeetCode 589. N-ary Tree Preorder Traversal</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-559-maximum-depth-of-n-ary-tree/">花花酱 LeetCode 559. Maximum Depth of N-ary Tree</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-559-maximum-depth-of-n-ary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 865. Smallest Subtree with all the Deepest Nodes</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-865-smallest-subtree-with-all-the-deepest-nodes/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-865-smallest-subtree-with-all-the-deepest-nodes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 08 Jul 2018 15:55:55 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[depth]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[subtree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3018</guid>

					<description><![CDATA[<p>Problem Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A node is deepest if it has the largest depth&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-865-smallest-subtree-with-all-the-deepest-nodes/">花花酱 LeetCode 865. Smallest Subtree with all the Deepest 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><iframe width="500" height="375" src="https://www.youtube.com/embed/q1zk8vZIDw0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given a binary tree rooted at <code>root</code>, the <em>depth</em> of each node is the shortest distance to the root.</p>
<p>A node is <em>deepest</em> if it has the largest depth possible among any node in the <u>entire tree</u>.</p>
<p>The subtree of a node is that node, plus the set of all descendants of that node.</p>
<p>Return the node with the largest depth such that it contains all the deepest nodes in it&#8217;s subtree.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false "><strong>Input: </strong><span id="example-input-1-1">[3,5,1,6,2,0,8,null,null,7,4]</span>
<strong>Output: </strong><span id="example-output-1">[2,7,4]</span>
<strong>Explanation:
</strong>
<img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" alt="" />

We return the node with value 2, colored in yellow in the diagram.
The nodes colored in blue are the deepest nodes of the tree.
The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
Both the input and output have TreeNode type.
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ul>
<li>The number of nodes in the tree will be between 1 and 500.</li>
<li>The values of each node are unique.</li>
</ul>
<p>&nbsp;</p>
<p><ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2404451723245401"
     data-ad-slot="7983117522"><br />
</ins></p>
<p><img class="alignnone size-full wp-image-3022" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/866-ep204.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/866-ep204.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/866-ep204-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/866-ep204-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
public:
  TreeNode* subtreeWithAllDeepest(TreeNode* root) {
    return depth(root).second;
  }
private:
  pair&lt;int, TreeNode*&gt; depth(TreeNode* root) {
    if (root == nullptr)
      return {-1, nullptr};
    auto l = depth(root-&gt;left);
    auto r = depth(root-&gt;right);
    int dl = l.first;
    int dr = r.first;
    return { max(dl, dr) + 1, 
             dl == dr ? root : (dl &gt; dr) ? l.second : r.second};
  }
};</pre><p>v2</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
public:
  TreeNode* subtreeWithAllDeepest(TreeNode* root) {
    TreeNode* ans;
    depth(root, &amp;ans);
    return ans;
  }
private:
  int depth(TreeNode* root, TreeNode** s) {
    if (root == nullptr)
      return -1;
    TreeNode* sl;
    TreeNode* sr;
    int l = depth(root-&gt;left, &amp;sl);
    int r = depth(root-&gt;right, &amp;sr);
    *s = (l == r) ? root : ((l &gt; r) ? sl : sr);
    return max(l, r) + 1;
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 40 ms
"""
class Solution:
  def subtreeWithAllDeepest(self, root):
    def solve(root):
      if not root: return (-1, None)
      dl, sl = solve(root.left)
      dr, sr = solve(root.right)
      if dl == dr: return (dl + 1, root)
      return (dl + 1, sl) if dl &gt; dr else (dr + 1, sr)
    return solve(root)[1]</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-865-smallest-subtree-with-all-the-deepest-nodes/">花花酱 LeetCode 865. Smallest Subtree with all the Deepest 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-865-smallest-subtree-with-all-the-deepest-nodes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
