Same Tree
2015-09-15 21:52
363 查看
【题目描述】
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
【思路】
一开始以为只要判断两棵树是否结构相同,wa了一次。
【代码】
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
【思路】
一开始以为只要判断两棵树是否结构相同,wa了一次。
【代码】
/** * 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 true; else if(p==NULL||q==NULL) return false; if(p->val!=q->val) return false; bool leftresult=isSameTree(p->left,q->left); bool rightresult=isSameTree(p->right,q->right); return leftresult&&rightresult; } };
相关文章推荐
- 细说 Form (表单)
- 通过FTP命令上传下载
- 设计模式-装饰者模式
- java解析二维码
- Objective-C 基础知识之(八):NSSet
- 对水波特效实现原理的解释
- Android多媒体---SurfaceView(播放视频)+Mediaplayer(音频)
- Android NDK的C++11标准支持
- linux ftps ftp-over-ssl 配置实验
- 第六天 :软件包管理
- iOS项目开发实战——使用ShareSDK进行QQ和微信分享
- POJ 2327 Dumb Bones
- 转发和重定向
- oracle部署中的三层结构
- iOS初级数据持久化 沙盒机制 归档与反归档
- 英语巴菲
- 张鑫旭抛物线
- iOS初级数据持久化 沙盒机制 归档与反归档
- c++11实现l延迟调用(惰性求值)
- Window8.1下安装IIS+PHP+Zend Optimizer + MySQL方法