Skip to content

Commit aebe30e

Browse files
committed
Add binary tree problems from Leetcode
1 parent 89b06bb commit aebe30e

3 files changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
3+
Given the root of a binary tree, return the inorder traversal of its nodes' values.
4+
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13+
* };
14+
*/
15+
class Solution {
16+
public:
17+
vector<int> inorderTraversal(TreeNode* root) {
18+
vector<int> inorder;
19+
inorderTraversal(root, inorder);
20+
return inorder;
21+
}
22+
23+
void inorderTraversal(TreeNode* root, vector<int> &inorder) {
24+
if (!root)
25+
return;
26+
inorderTraversal(root->left, inorder);
27+
inorder.push_back(root->val);
28+
inorderTraversal(root->right, inorder);
29+
}
30+
};

Tree/(LEETCODE) SameTree.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
3+
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
4+
5+
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
6+
7+
* Definition for a binary tree node.
8+
* struct TreeNode {
9+
* int val;
10+
* TreeNode *left;
11+
* TreeNode *right;
12+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
14+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
bool isSameTree(TreeNode* p, TreeNode* q) {
20+
if (p == NULL && q == NULL)
21+
return true;
22+
if (p == NULL || q == NULL)
23+
return false;
24+
if (p->val == q->val) {
25+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
26+
}
27+
return false;
28+
}
29+
};

Tree/(LEETCODE) SymmetricTree.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
3+
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
4+
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13+
* };
14+
*/
15+
class Solution {
16+
public:
17+
bool isSymmetric(TreeNode* root) {
18+
if (root == NULL)
19+
return true;
20+
return isSymmetric(root->left, root->right);
21+
}
22+
23+
bool isSymmetric(TreeNode* p, TreeNode* q) {
24+
if (p == NULL && q == NULL)
25+
return true;
26+
if (p == NULL || q == NULL)
27+
return false;
28+
29+
if (p->val == q->val) {
30+
return isSymmetric(p->left, q->right) && isSymmetric(p->right, q->left);
31+
}
32+
return false;
33+
}
34+
};

0 commit comments

Comments
 (0)