LeetCode--Same Tree
2015-11-03 11:08
411 查看
题目:
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.
代码:
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) {} * }; */
bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL && q == NULL) { return true; } else if(p == NULL && q != NULL) { return false; } else if( p != NULL && q == NULL) { return false; } else { if(p->val == q->val) { return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } else return false; } }
相关文章推荐
- 水仙花数
- stash存储
- BZOJ 1019: [SHOI2008]汉诺塔( dp )
- .net 响应文件头处理
- $(srctree) is not clean, please run 'make mrproper'
- 百度编辑器 UEditor 使用
- (仿360卸载后弹窗)Android卸载程序之后跳转到指定的反馈页面
- HDU 5534(Partial Tree-背包)
- Android常用的编码操作规范
- 利用JDBC连接服务器数据库
- InstallShield使用---安装设计界面
- 韩信点兵
- undefined reference to
- Asp.Net MVC 利用ReflectedActionDescriptor判断Action返回类型
- 自定义datagridview列,却提示DataGridView 控件中至少有一列没有单元格模板
- streamreader
- 【173】双显示器随便切换位置
- MySQL主从复制
- MySQL导出数据到txt
- 电缆快速检测系统硬件设计