Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool helper(TreeNode* t1, TreeNode* t2){
if(t1 == nullptr && t2==nullptr){
return true;
}
if(t1 == nullptr || t2 == nullptr){
return false;
}
return (t1->val == t2->val)
&& helper(t1->right, t2->left)
&& helper(t1->left, t2->right);
}
bool isSymmetric(TreeNode* root) {
return helper(root, root);
}
};
'알고리즘 문제풀이 > leetcode' 카테고리의 다른 글
[leetcode 200] Number of Islands (0) | 2020.10.23 |
---|---|
[leetcode 207] Course Schedule (0) | 2020.10.23 |
[leetcode 79] Word Search (0) | 2020.10.22 |
[leetcode 121] Best Time to Buy and Sell Stock (0) | 2020.10.22 |
[leetcode 114] Flatten Binary Tree to Linked List (0) | 2020.10.21 |
댓글