<?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>same tree Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/same-tree/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/same-tree/</link>
	<description></description>
	<lastBuildDate>Sat, 15 Dec 2018 09:14:54 +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>same tree Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/same-tree/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 572. Subtree of Another Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-572-subtree-of-another-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-572-subtree-of-another-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 20 Mar 2018 04:34:25 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[same tree]]></category>
		<category><![CDATA[subtree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2239</guid>

					<description><![CDATA[<p>Problem 题目大意：判断一棵树是不是另外一棵树的子树。 https://leetcode.com/problems/subtree-of-another-tree/description/ Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-572-subtree-of-another-tree/">花花酱 LeetCode 572. Subtree of Another 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>题目大意：判断一棵树是不是另外一棵树的子树。</p>
<p><a href="https://leetcode.com/problems/subtree-of-another-tree/description/">https://leetcode.com/problems/subtree-of-another-tree/description/</a></p>
<p>Given two non-empty binary trees <b>s</b> and <b>t</b>, check whether tree <b>t</b> has exactly the same structure and node values with a subtree of <b>s</b>. A subtree of <b>s</b> is a tree consists of a node in <b>s</b> and all of this node&#8217;s descendants. The tree <b>s</b> could also be considered as a subtree of itself.</p>
<p><b>Example 1:</b><br />
Given tree s:</p><pre class="crayon-plain-tag">3
    / \
   4   5
  / \
 1   2</pre><p>Given tree t:</p><pre class="crayon-plain-tag">4 
  / \
 1   2</pre><p>Return <b>true</b>, because t has the same structure and node values with a subtree of s.</p>
<p><b>Example 2:</b><br />
Given tree s:</p><pre class="crayon-plain-tag">3
    / \
   4   5
  / \
 1   2
    /
   0</pre><p>Given tree t:</p><pre class="crayon-plain-tag">4
  / \
 1   2</pre><p>Return <b>false</b>.</p>
<h1>Solution: Recursion</h1>
<p>Time complexity: O(max(n, m))</p>
<p>Space complexity: O(max(n, m))</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 29 ms
class Solution {
public:
  bool isSubtree(TreeNode* s, TreeNode* t) {
    if (t == nullptr) return true;
    if (s == nullptr) return false;
    if (isSameTree(s, t)) return true;
    return isSubtree(s-&gt;left, t) || isSubtree(s-&gt;right, t);
  }
private:
  bool isSameTree(TreeNode* s, TreeNode* t) {
    if (s == nullptr &amp;&amp; t == nullptr) return true;
    if (s == nullptr || t == nullptr) return false;
    return (s-&gt;val == t-&gt;val) 
           &amp;&amp; isSameTree(s-&gt;left, t-&gt;left) 
           &amp;&amp; isSameTree(s-&gt;right, t-&gt;right);
  }
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-100-same-tree/">[解题报告] Leetcode 100. Same Tree</a></li>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-508-most-frequent-subtree-sum/">花花酱 LeetCode 508. Most Frequent Subtree Sum</a></li>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-563-binary-tree-tilt/">花花酱 LeetCode 563. Binary Tree Tilt</a></li>
</ul>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-572-subtree-of-another-tree/">花花酱 LeetCode 572. Subtree of Another 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-572-subtree-of-another-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 Leetcode 100. Same Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-100-same-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-100-same-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 02 Sep 2017 23:42:37 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[same tree]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=45</guid>

					<description><![CDATA[<p>Problem: Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-100-same-tree/">花花酱 Leetcode 100. Same 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[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/VJVQpqPRptM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<h1><strong>Problem:</strong></h1>
<p>Given two binary trees, write a function to check if they are the same or not.</p>
<p>Two binary trees are considered the same if they are structurally identical and the nodes have the same value.</p>
<h2><b>Example 1:</b></h2>
<pre class="crayon:false">Input:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true
</pre>
<h2><b>Example 2:</b></h2>
<pre class="crayon:false">Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false
</pre>
<h2><b>Example 3:</b></h2>
<pre class="crayon:false">Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false
</pre>
<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"> </ins></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
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        // Both are emtry: same
        if (!p &amp;&amp; !q) return true;
        // One is emtry: not the Same
        if (!p || !q) return false;
        // Both are not emptry, compare the root value
        if (p-&gt;val != q-&gt;val) return false;
        // Compare left-subtree and right-subtree recursively.
        return isSameTree(p-&gt;left, q-&gt;left) 
            &amp;&amp; isSameTree(p-&gt;right, q-&gt;right); 
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public boolean isSameTree(TreeNode p, TreeNode q) {
    if (p == null &amp;&amp; q == null) return true;
    if (p == null || q == null) return false;
    return p.val == q.val &amp;&amp; isSameTree(p.left, q.left) &amp;&amp; isSameTree(p.right, q.right);
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
"""
class Solution:
  def isSameTree(self, p, q):
    if not p and not q: return True
    if not p or not q: return False
    return all((p.val == q.val, 
               self.isSameTree(p.left, q.left), 
               self.isSameTree(p.right, q.right)))</pre><p></div></div></p>
<h1><strong>Follow Up</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-572-subtree-of-another-tree/">花花酱 LeetCode 572. Subtree of Another Tree</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-100-same-tree/">花花酱 Leetcode 100. Same 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-100-same-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
