36-Same Tree
2024-09-04 21:53:26
- Same Tree My Submissions QuestionEditorial Solution
Total Accepted: 126116 Total Submissions: 291884 Difficulty: Easy
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Subscribe to see which companies asked this question
思路:太简单,主要p,q为空考虑到,并用一行代码实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL||q==NULL)return p==q;
if(p->val==q->val)
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
else return false;
}
};
最新文章
- LINQ to SQL Where条件
- 重复加载同一个jqgrid
- Unity3D上可以发布到IOS使用的SQLite数据库
- 关于ApplicationPoolIdentity
- C#中位、字节等知识
- django 学习-1
- bzoj2242
- ZendFramework使用中常见问题
- tomcat发布静态网页
- C 语言中的变量为什么不能以数字打头
- C++中 auto自己主动变量,命名空间,using作用以及作用域
- 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException
- LVS的DR设置测试
- centos7环境搭建命令List
- 左耳听风-ARTS-第3周(2019/4/7-2019/4/13)
- Hashtable与Dictionary比较
- 配置cron定时任务
- 操作系统学习笔记(二) 页式映射及windbg验证方式
- unwrap bug
- Nowcoder 提高组练习赛-R7