-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100.SameTree.cpp
More file actions
32 lines (30 loc) · 1.04 KB
/
100.SameTree.cpp
File metadata and controls
32 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Question:
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.
*/
/* 思路:
1)两棵树都为空,说明前面的节点全部相同,两棵树相等,返回true
2)p或q任何一个为空,另一个不为空,返回false
3)p和q所指向的节点的值不同,返回false
4)分别递归判断p和q所指向的左右子树是否相等
*/
// Code:
/**
* 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 == nullptr && q == nullptr) return true;
if(p == nullptr && q != nullptr) return false;
if(p != nullptr && q == nullptr) return false;
if(p->val != q->val) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};