100:相同的树
This commit is contained in:
parent
74562a9aca
commit
12cd4221c6
88
src/main/java/leetcode/editor/cn/SameTree.java
Normal file
88
src/main/java/leetcode/editor/cn/SameTree.java
Normal file
@ -0,0 +1,88 @@
|
||||
//给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
|
||||
//
|
||||
// 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:p = [1,2,3], q = [1,2,3]
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:p = [1,2], q = [1,null,2]
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:p = [1,2,1], q = [1,1,2]
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 两棵树上的节点数目都在范围 [0, 100] 内
|
||||
// -10⁴ <= Node.val <= 10⁴
|
||||
//
|
||||
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 776 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
//100:相同的树
|
||||
public class SameTree {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new SameTree().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 isSameTree(TreeNode p, TreeNode q) {
|
||||
if (p == null && q == null) {
|
||||
return true;
|
||||
}
|
||||
if (p == null || q == null) {
|
||||
return false;
|
||||
}
|
||||
if (p.val != q.val) {
|
||||
return false;
|
||||
}
|
||||
if (!isSameTree(p.left, q.left)) {
|
||||
return false;
|
||||
}
|
||||
if (!isSameTree(p.right, q.right)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
36
src/main/java/leetcode/editor/cn/doc/content/SameTree.md
Normal file
36
src/main/java/leetcode/editor/cn/doc/content/SameTree.md
Normal file
@ -0,0 +1,36 @@
|
||||
<p>给你两棵二叉树的根节点 <code>p</code> 和 <code>q</code> ,编写一个函数来检验这两棵树是否相同。</p>
|
||||
|
||||
<p>如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg" style="width: 622px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>p = [1,2,3], q = [1,2,3]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg" style="width: 382px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>p = [1,2], q = [1,null,2]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg" style="width: 622px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>p = [1,2,1], q = [1,1,2]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>两棵树上的节点数目都在范围 <code>[0, 100]</code> 内</li>
|
||||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 776</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user