LeetCode 100:Same Tree
2015-12-07 11:16
344 查看
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 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&&!q)
return true;
else if (p&&!q)
return false;
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);
}
}
};
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
/**
* 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&&!q)
return true;
else if (p&&!q)
return false;
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);
}
}
};
相关文章推荐
- Mina源码阅读笔记(三)-Mina的连接IoAccpetor
- jsp获取websocket的远程服务器地址
- 利用eclipse修改不同类型文件的编码格式
- sap IUT255 Integration of SAP CRM and SAP IS-U_EN_Col62.pdf
- Spring MVC拦截器+注解方式实现防止表单重复提交
- psd页面切割成html技巧总结
- 程序员面试问题搜集
- Python语言assert用法
- 保存防火墙规则的方法
- “解决问题”与“消灭问题”
- Jenkins学习之——(4)Email Extension Plugin插件的配置与使用
- exit return
- iOS7.0中UILabel高度调整注意事项(转)
- Android Handler Leak
- 触发器
- 黑马程序员——C基础之石头剪刀布游戏
- POJ1182食物链,HDU1829(种类并查集)
- windows下 composer常见问题及处理
- 10007---谈谈Memcached与Redis
- 栈溢出利用--预备知识