-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path101.cpp
More file actions
33 lines (32 loc) · 1.04 KB
/
101.cpp
File metadata and controls
33 lines (32 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
33
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 101
// Title: Symmetric Tree
// Link: https://leetcode.com/problems/symmetric-tree
// Idea: The recursion is a bit tricky, as you have to recur over two nodes at
// a time. But when you have two nodes, one on the left, and one on the right,
// check that left->left mirrors right->right, and left->right mirrors
// right->left.
// Difficulty: medium
// 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 mirror(TreeNode* left, TreeNode* right) {
if (left == nullptr ^ right == nullptr) return false;
if (left == nullptr & right == nullptr) return true;
return left->val == right->val && mirror(left->left, right->right) &&
mirror(left->right, right->left);
}
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return mirror(root->left, root->right);
}
};