[leetcode] 100. Same Tree 解题报告
2015-12-24 15:40
453 查看
题目链接:https://leetcode.com/problems/same-tree/
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.
思路:同时遍历一遍树,发现任何一个节点不相等则两棵树不相等。写水题的感觉好爽
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
代码如下:
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.
思路:同时遍历一遍树,发现任何一个节点不相等则两棵树不相等。写水题的感觉好爽
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
代码如下:
/** * 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; if((p == NULL) ^ (q == NULL)) return false; if(p->val != q->val) return false; return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };
相关文章推荐
- Objective-C:Foundation框架-常用类-NSNull
- Swift中的Weak Strong Dance
- jdbc 开启事务
- AES--高级数据加密标准
- Webpack打包进阶
- (NO.00005)iOS实现炸弹人游戏(七):游戏数据的序列化表示
- php创建微信二维码40052错误提示
- 易企CMS文章/产品标题太长怎么办?
- Linux screen 使用
- go中的main函数和init函数
- (NO.00005)iOS实现炸弹人游戏(七):游戏数据的序列化表示
- (NO.00005)iOS实现炸弹人游戏(七):游戏数据的序列化表示
- freemarker的空值和默认值
- android studio 混淆 多dex
- WEB前端开发人员须知的常见浏览器兼容问题及解决技巧
- 链表的创建和添加元素
- Objective-C:Foundation框架-常用类-NSValue
- jackson的使用
- math方法集合
- 如何在IntelliJ IDEA 15.02中构建scala开发环境