-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path100.cpp
More file actions
25 lines (25 loc) · 679 Bytes
/
100.cpp
File metadata and controls
25 lines (25 loc) · 679 Bytes
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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 100
// Title: Same Tree
// Link: https://leetcode.com/problems/same-tree
// Idea: Use recursion.
// Difficulty: easy
// Tags: binary-tree
/**
* 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 false;
if (p == nullptr & q == nullptr) return true;
return p->val == q->val && isSameTree(p->left, q->left) &&
isSameTree(p->right, q->right);
}
};