Same Tree
2024-09-16 14:05:10
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.
/**
* Definition for binary tree
* 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 && !q)
return true;
else if(!p || !q)
return false;
else if(p->val != q->val)
return false;
else
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
};
递归。
最新文章
- 阅读摘录《javascript 高级程序设计》01
- Object.prototype.toString.call()进行类型判断
- 初探YAML
- JavaScript中的逗号运算符
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
- HBase HFileBlock
- Oracle EBS-SQL (INV-1):检查物料成本为0并且物料状态不是'NEW'的物料.sql
- eclipse中maven工程的创建javaweb项目
- 前端JS面试题汇总 Part 3 (宿主对象与原生对象/函数调用方式/call与apply/bind/document.write)
- java 反射的简介
- Perl:undef类型和defined()函数
- 浅谈压缩感知(二十一):压缩感知重构算法之正交匹配追踪(OMP)
- JdbcTemplate查询返回JavaBean的几种方法
- SEO优化上首页之搜索引擎排名规则
- 点击图片查看大图(纯js)
- java 对象
- java基础二 java的跨平台特性
- SDWebImage缓存图片的机制
- sqlite数据库下载安装和初步操作和所遇到的问题near &;quot;sqlite3&;quot;:syntax error
- 网页方式访问 QQ 小说书架