leetcode 100 —— Same Tree
2015-08-08 16:25
330 查看
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.
思路:先序遍历,可以设置flag,提前返回。
class Solution {
bool flag = true;
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
preOrder(p, q);
return flag;
}
void preOrder(TreeNode* p, TreeNode *q){
if (!flag||(!p&&!q)) return;
if ((!p&&q) || (p&&!q) || (p->val != q->val)){
flag = false;
return;
}
preOrder(p->left, q->left);
preOrder(p->right, q->right);
}
};
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
思路:先序遍历,可以设置flag,提前返回。
class Solution {
bool flag = true;
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
preOrder(p, q);
return flag;
}
void preOrder(TreeNode* p, TreeNode *q){
if (!flag||(!p&&!q)) return;
if ((!p&&q) || (p&&!q) || (p->val != q->val)){
flag = false;
return;
}
preOrder(p->left, q->left);
preOrder(p->right, q->right);
}
};
相关文章推荐
- dz7.2升级到dzx2
- linux 下 c++ 多线程的实现
- Android性能优化典范 - 第2季
- Android ANR产生原因和解决办法
- [leetcode] Container With Most Water
- TCP、UDP、Socket
- OC循环渐进:代理设计模式
- angular.js笔记 ---关于Route路由
- android中的任务栈和启动模式的总结
- python 正则表达式
- hdu 2063 过山车(匈牙利算法模板)
- 如何快速提高自己的开发能力
- Android之GridView横向左右滚动
- 线程池 c 实现
- 《啪啪三国》技术详解
- Android EventBus现实 听说你out该
- 关于递归
- LeetCode(82)题解: Remove Duplicates from Sorted List II
- Android性能优化典范
- android服务的总结