Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117336
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-24 16:22
文章存档

2014年(53)

我的朋友

分类: C/C++

2014-12-06 15:42:18

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.


很像那个镜像的树,思想完全一样。


  1. bool isSameTree(TreeNode *p, TreeNode *q) {
  2.     if(p==NULL && q==NULL)
  3.         return true;
  4.     if(p==NULL || q==NULL)
  5.         return false;
  6.     return p->val==q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
  7. }


阅读(802) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~