Skip to content

Commit

Permalink
LeetCode: 101. Symmetric Tree
Browse files Browse the repository at this point in the history
- accepted;
  • Loading branch information
olegon committed Aug 8, 2023
1 parent 0b1f4ae commit b5ff6c6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions leetcode/problems/symmetric-tree/compile-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -x

clang++ main.cpp -W -Wall -std=c++17 -O2 -fsanitize=address && ./a.out
38 changes: 38 additions & 0 deletions leetcode/problems/symmetric-tree/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
101. Symmetric Tree
https://leetcode.com/problems/symmetric-tree
*/

#include <bits/stdc++.h>

using namespace std;

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 isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
else return isSymmetric(root->right, root->left);
}

bool isSymmetric(TreeNode* p, TreeNode* q) {
if (p == nullptr && q == nullptr) return true;
else if (p == nullptr) return false;
else if (q == nullptr) return false;
else return p->val == q->val && isSymmetric(p->left, q->right) && isSymmetric(p->right, q->left);
}
};

int main(void) {
ios::sync_with_stdio(false);

return EXIT_SUCCESS;
}

0 comments on commit b5ff6c6

Please sign in to comment.