572:另一棵树的子树

This commit is contained in:
huangge1199 2021-08-16 14:34:21 +08:00
parent edd7a03582
commit 3cf252644c
2 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,89 @@
//
//
// 给你两棵二叉树 root subRoot 检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树如果存在返回 true 否则
//返回 false
//
// 二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点tree 也可以看做它自身的一棵子树
//
//
//
// 示例 1
//
//
//输入root = [3,4,5,1,2], subRoot = [4,1,2]
//输出true
//
//
// 示例 2
//
//
//输入root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
//输出false
//
//
//
//
// 提示
//
//
// root 树上的节点数量范围是 [1, 2000]
// subRoot 树上的节点数量范围是 [1, 1000]
// -104 <= root.val <= 104
// -104 <= subRoot.val <= 104
//
//
//
// Related Topics 深度优先搜索 二叉树 字符串匹配 哈希函数
// 👍 539 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
//572:另一棵树的子树
class SubtreeOfAnotherTree {
public static void main(String[] args) {
//测试代码
Solution solution = new SubtreeOfAnotherTree().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) {
return false;
}
return check(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
public boolean check(TreeNode root, TreeNode subRoot) {
if (root == null && subRoot == null) {
return true;
}
if (root == null || subRoot == null || root.val != subRoot.val) {
return false;
}
return check(root.left, subRoot.left) && check(root.right, subRoot.right);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,35 @@
<div class="original__bRMd">
<div>
<p>给你两棵二叉树 <code>root</code><code>subRoot</code> 。检验 <code>root</code> 中是否包含和 <code>subRoot</code> 具有相同结构和节点值的子树。如果存在,返回 <code>true</code> ;否则,返回 <code>false</code></p>
<p>二叉树 <code>tree</code> 的一棵子树包括 <code>tree</code> 的某个节点和这个节点的所有后代节点。<code>tree</code> 也可以看做它自身的一棵子树。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/subtree1-tree.jpg" style="width: 532px; height: 400px;" />
<pre>
<strong>输入:</strong>root = [3,4,5,1,2], subRoot = [4,1,2]
<strong>输出:</strong>true
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/subtree2-tree.jpg" style="width: 502px; height: 458px;" />
<pre>
<strong>输入:</strong>root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
<strong>输出:</strong>false
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>root</code> 树上的节点数量范围是 <code>[1, 2000]</code></li>
<li><code>subRoot</code> 树上的节点数量范围是 <code>[1, 1000]</code></li>
<li><code>-10<sup>4</sup> <= root.val <= 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> <= subRoot.val <= 10<sup>4</sup></code></li>
</ul>
</div>
</div>
<div><div>Related Topics</div><div><li></li><li>深度优先搜索</li><li>二叉树</li><li>字符串匹配</li><li>哈希函数</li></div></div>\n<div><li>👍 539</li><li>👎 0</li></div>