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);
}
};

递归。

最新文章

  1. 阅读摘录《javascript 高级程序设计》01
  2. Object.prototype.toString.call()进行类型判断
  3. 初探YAML
  4. JavaScript中的逗号运算符
  5. Educational Codeforces Round 11 C. Hard Process 前缀和+二分
  6. HBase HFileBlock
  7. Oracle EBS-SQL (INV-1):检查物料成本为0并且物料状态不是'NEW'的物料.sql
  8. eclipse中maven工程的创建javaweb项目
  9. 前端JS面试题汇总 Part 3 (宿主对象与原生对象/函数调用方式/call与apply/bind/document.write)
  10. java 反射的简介
  11. Perl:undef类型和defined()函数
  12. 浅谈压缩感知(二十一):压缩感知重构算法之正交匹配追踪(OMP)
  13. JdbcTemplate查询返回JavaBean的几种方法
  14. SEO优化上首页之搜索引擎排名规则
  15. 点击图片查看大图(纯js)
  16. java 对象
  17. java基础二 java的跨平台特性
  18. SDWebImage缓存图片的机制
  19. sqlite数据库下载安装和初步操作和所遇到的问题near "sqlite3":syntax error
  20. 网页方式访问 QQ 小说书架

热门文章

  1. TCP和UDP之间的区别
  2. 一个Linq表达式的扩展函数帮助类
  3. Json的序列化和反序列化
  4. Winfrom中ListBox绑定List数据源更新问题
  5. 以对象的方式来访问xml数据表(二)
  6. 【BZOJ 4326】【NOIP2015】运输计划
  7. yii2时间日期控件的使用[转]
  8. Java中处理异常throw和throws
  9. jdk1.8 J.U.C之FutureTask实现机制分析
  10. jquery对javascript事件的封装一览