[LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true
Example 2:
Input: 1 1
/ \
2 2 [1,2], [1,null,2] Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
给2个二叉树,写一个函数来判断它们是否是相同的。
解法:递归。当前节点相等,且他们左子树和右子树都相等,要考虑到树为Null的情况。基于先序,中序或者后序遍历都可以做完成,因为对遍历顺序没有要求。这里主要考虑一下结束条件,如果两个结点都是null,也就是到头了,那么返回true。如果其中一个是null,说明在一棵树上结点到头,另一棵树结点还没结束,即树不相同,或者两个结点都非空,并且结点值不相同,返回false。最后递归处理两个结点的左右子树,返回左右子树递归的与结果即可。这里使用的是先序遍历,算法的复杂度跟遍历是一致的,如果使用递归,时间复杂度是O(n),空间复杂度是O(logn)。
Java:
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;
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
Python:
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
if p is None and q is None:
return True if p is not None and q is not None:
return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) return False
C++:
class Solution {
bool check(TreeNode* p, TreeNode* q) {
// 若两个指针均为空
if (!p && !q) return true;
// 若只有一个指针为空
if (!p || !q) return false;
// 两个指针都不为空,则比较值
if (p -> val != q -> val) return false;
// 递归比较子节点
return check(p -> left, q -> left) && check(p -> right, q -> right);
}
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return check(p, q);
}
};
类似题目:
[LeetCode] 110. Balanced Binary Tree 平衡二叉树
All LeetCode Questions List 题目汇总
[LeetCode] 100. Same Tree 相同树的更多相关文章
- [LeetCode]100. Same Tree判断树相同
dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode]100. Same Tree相同的树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 100. Same Tree相同的树 (C++)
题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- leetcode 100 Same Tree ----- java
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- BLE——协议层次结构
未完待续…… BLE协议 Bluetooth Application Applications GATT-Based Profiles/Services Bluetooth Core (Stack) ...
- 性能测试基础---URL和HTTP协议
·URL和HTTP协议: ·URL构成: URL是web应用进行资源访问的主要方式.一般来说,由五个部分构成: 示例:http://192.168.2.212/phpwind1/searcher.ph ...
- dockerhub下载加速
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f5dad4ec.m.daocloud.io syste ...
- python - django 执行数据库迁移后数据库并未更新 和 InternalError: (1054, u"Unknown column 'xxx' in 'field list'")问题
一.发生情况:当你修改数据库结构后进行 python manage.py makemigrations 和 python manage.py migrate 后发现控制台会给你返回一个下面的结果,但是 ...
- 是Mscoreei.dll的正确版本吗?
在安装.NET 4.0或更高版本之后,您可能会注意到.NET进程有点不寻常.下面是用.NET 2.0编译器编译的简单“Hello World”可执行文件的加载模块的部分列表. 开始-结束模块名称 60 ...
- Python 02 编写代码
原文:https://www.cnblogs.com/jimmy-share/p/9784219.html 方式: 交互式编程:打开python.exe文件后,直接输入代码即可.文件的位置(我本地): ...
- [RN] React Native 常见基本问题归纳总结
[RN] React Native 常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...
- 【luoguP2371】 [国家集训队]墨墨的等式
题目链接 考虑将所有的\(a_1x_1+a_2x_2+--+a_nx_n=B\)对\(a_1\)取模,那么所有可达到的B就分为了\(0\)~\(a_1-1\)类 如果对\(a_1\)取模为\(k\)的 ...
- 【Codeforces】CF367D Sereja and Sets (数学)
题目大意 1到n这n个正整数被分成了m个不相交的集合(集合不一定连续),现在从这m个集合中选出最少个数的集合,满足对于[1,n]中任意一个长度为d的区间都至少有一个数字出现在已选集合中.(1 < ...
- 配置docker阿里云加速器
1. 安装/升级Docker客户端 推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce 2. 配置镜像加速器 针对Docker客户端版本大于 1.10.0 的用户 您可以通 ...