/*
* Leetcode100. Same Tree
* Funtion: Given two binary trees, write a function to check if they are equal or not.
* Author: LKJ
* Date:2016/7/21
* 找两棵数是不是一样的
*/
#include <iostream>
//#include <vector>
using namespace std;
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;
}else if( (p == NULL)&&(q != NULL) ){
return false;
}else{
if(p->val != q->val){
return false;
}else{
return (isSameTree(p->left,q->left) && isSameTree(p->right,q->right));
}
}
}
};
int main(){
//int myin;
//int myout;
//Solution SA;
//cout << "Please Enter" << endl;
//cin >> myin;
//myout = SA.singleNumber(myin);
//cout << myout << endl;
//cout << getFristBit(1) << endl;
return 0;
}
LeetCode 二叉树 | 100. Same Tree
最新推荐文章于 2024-10-14 18:27:49 发布
本文介绍了一个解决LeetCode第100题的方法——判断两棵二叉树是否相同。通过递归的方式比较两棵树的节点值及左右子树,最终得出结论。
1137

被折叠的 条评论
为什么被折叠?



