Skip to content

Commit 0f10aea

Browse files
authored
Add files via upload
1 parent 7bc156c commit 0f10aea

File tree

50 files changed

+3540
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3540
-0
lines changed

100.相同的树.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* @lc app=leetcode.cn id=100 lang=cpp
3+
*
4+
* [100] 相同的树
5+
*
6+
* https://leetcode-cn.com/problems/same-tree/description/
7+
*
8+
* algorithms
9+
* Easy (55.48%)
10+
* Likes: 249
11+
* Dislikes: 0
12+
* Total Accepted: 44.5K
13+
* Total Submissions: 80.2K
14+
* Testcase Example: '[1,2,3]\n[1,2,3]'
15+
*
16+
* 给定两个二叉树,编写一个函数来检验它们是否相同。
17+
*
18+
* 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
19+
*
20+
* 示例 1:
21+
*
22+
* 输入: 1 1
23+
* ⁠ / \ / \
24+
* ⁠ 2 3 2 3
25+
*
26+
* ⁠ [1,2,3], [1,2,3]
27+
*
28+
* 输出: true
29+
*
30+
* 示例 2:
31+
*
32+
* 输入: 1 1
33+
* ⁠ / \
34+
* ⁠ 2 2
35+
*
36+
* ⁠ [1,2], [1,null,2]
37+
*
38+
* 输出: false
39+
*
40+
*
41+
* 示例 3:
42+
*
43+
* 输入: 1 1
44+
* ⁠ / \ / \
45+
* ⁠ 2 1 1 2
46+
*
47+
* ⁠ [1,2,1], [1,1,2]
48+
*
49+
* 输出: false
50+
*
51+
*
52+
*/
53+
54+
// @lc code=start
55+
/**
56+
* Definition for a binary tree node.
57+
* struct TreeNode {
58+
* int val;
59+
* TreeNode *left;
60+
* TreeNode *right;
61+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
62+
* };
63+
*/
64+
class Solution {
65+
public:
66+
bool isSameTree(TreeNode* p, TreeNode* q) {
67+
vector<int>pnums;
68+
vector<int>qnums;
69+
pnums = num(p, pnums);
70+
qnums = num(q, qnums);
71+
if (pnums.size() != qnums.size()) return false;
72+
else {
73+
for (int i = 0; i<pnums.size(); i++) {
74+
if (pnums[i] != qnums[i]) return false;
75+
}
76+
}
77+
return true;
78+
}
79+
80+
vector<int> num(TreeNode* p, vector<int>& nums) {
81+
TreeNode* root = p;
82+
if (!root) {
83+
nums.push_back(-5);
84+
return nums;
85+
}
86+
else {
87+
nums.push_back(root->val);
88+
num(root->left, nums);
89+
num(root->right, nums);
90+
}
91+
return nums;
92+
}
93+
};
94+
// @lc code=end
95+

101.对称二叉树.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=cpp
3+
*
4+
* [101] 对称二叉树
5+
*
6+
* https://leetcode-cn.com/problems/symmetric-tree/description/
7+
*
8+
* algorithms
9+
* Easy (48.91%)
10+
* Likes: 488
11+
* Dislikes: 0
12+
* Total Accepted: 66.7K
13+
* Total Submissions: 136.3K
14+
* Testcase Example: '[1,2,2,3,4,4,3]'
15+
*
16+
* 给定一个二叉树,检查它是否是镜像对称的。
17+
*
18+
* 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
19+
*
20+
* ⁠ 1
21+
* ⁠ / \
22+
* ⁠ 2 2
23+
* ⁠/ \ / \
24+
* 3 4 4 3
25+
*
26+
*
27+
* 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
28+
*
29+
* ⁠ 1
30+
* ⁠ / \
31+
* ⁠ 2 2
32+
* ⁠ \ \
33+
* ⁠ 3 3
34+
*
35+
*
36+
* 说明:
37+
*
38+
* 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
39+
*
40+
*/
41+
42+
// @lc code=start
43+
/**
44+
* Definition for a binary tree node.
45+
* struct TreeNode {
46+
* int val;
47+
* TreeNode *left;
48+
* TreeNode *right;
49+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
50+
* };
51+
*/
52+
class Solution {
53+
public:
54+
bool isSymmetric(TreeNode* root) {
55+
return ismerror(root, root);
56+
}
57+
bool ismerror(TreeNode* t1, TreeNode* t2){
58+
if(t1==NULL&&t2==NULL) return true;
59+
if(t1==NULL||t2==NULL) return false;
60+
return(t1->val==t2->val)&&ismerror(t1->left, t2->right)&&ismerror(t2->left,t1->right);
61+
}
62+
};
63+
// @lc code=end
64+

102.二叉树的层次遍历.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode.cn id=102 lang=cpp
3+
*
4+
* [102] 二叉树的层次遍历
5+
*
6+
* https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (59.24%)
10+
* Likes: 296
11+
* Dislikes: 0
12+
* Total Accepted: 51.8K
13+
* Total Submissions: 87.4K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
17+
*
18+
* 例如:
19+
* 给定二叉树: [3,9,20,null,null,15,7],
20+
*
21+
* ⁠ 3
22+
* ⁠ / \
23+
* ⁠ 9 20
24+
* ⁠ / \
25+
* ⁠ 15 7
26+
*
27+
*
28+
* 返回其层次遍历结果:
29+
*
30+
* [
31+
* ⁠ [3],
32+
* ⁠ [9,20],
33+
* ⁠ [15,7]
34+
* ]
35+
*
36+
*
37+
*/
38+
39+
// @lc code=start
40+
/**
41+
* Definition for a binary tree node.
42+
* struct TreeNode {
43+
* int val;
44+
* TreeNode *left;
45+
* TreeNode *right;
46+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
47+
* };
48+
*/
49+
class Solution {
50+
public:
51+
vector<vector<int>> levelOrder(TreeNode* root) {
52+
vector<vector<int>> ans;
53+
queue<TreeNode*> subtree;
54+
if(root){
55+
subtree.push(root);
56+
levelth(subtree, ans);
57+
}
58+
return ans;
59+
}
60+
61+
void levelth(queue<TreeNode*> &subtree, vector<vector<int>> &ans){
62+
TreeNode *cur;
63+
while(!subtree.empty()){
64+
vector<int> level;
65+
int width = subtree.size();
66+
for(int i=0; i<width; i++){
67+
cur = subtree.front();
68+
level.push_back(cur->val);
69+
subtree.pop();
70+
if(cur->left) subtree.push(cur->left);
71+
if(cur->right) subtree.push(cur->right);
72+
}
73+
ans.push_back(level);
74+
}
75+
}
76+
77+
};
78+
// @lc code=end
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @lc app=leetcode id=105 lang=cpp
3+
*
4+
* [105] Construct Binary Tree from Preorder and Inorder Traversal
5+
*
6+
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (43.01%)
10+
* Likes: 2043
11+
* Dislikes: 56
12+
* Total Accepted: 252.7K
13+
* Total Submissions: 587.5K
14+
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
15+
*
16+
* Given preorder and inorder traversal of a tree, construct the binary tree.
17+
*
18+
* Note:
19+
* You may assume that duplicates do not exist in the tree.
20+
*
21+
* For example, given
22+
*
23+
*
24+
* preorder = [3,9,20,15,7]
25+
* inorder = [9,3,15,20,7]
26+
*
27+
* Return the following binary tree:
28+
*
29+
*
30+
* ⁠ 3
31+
* ⁠ / \
32+
* ⁠ 9 20
33+
* ⁠ / \
34+
* ⁠ 15 7
35+
*
36+
*/
37+
/**
38+
* Definition for a binary tree node.
39+
* struct TreeNode {
40+
* int val;
41+
* TreeNode *left;
42+
* TreeNode *right;
43+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
44+
* };
45+
*/
46+
class Solution {
47+
public:
48+
int pre=0;
49+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
50+
return subtree(preorder, inorder, 0, preorder.size()-1);
51+
}
52+
53+
TreeNode* subtree(vector<int>& preorder, vector<int>& inorder, int l, int r){
54+
if(l > r) return NULL;
55+
if(l == r) return new TreeNode(preorder[pre++]);
56+
57+
TreeNode* root = new TreeNode(preorder[pre++]);
58+
int id = find(inorder.begin()+l, inorder.begin()+l+r, preorder[pre-1]) - inorder.begin();
59+
root->left = subtree(preorder, inorder, l, id-1);
60+
root->right = subtree(preorder, inorder, id+1, r);
61+
return root;
62+
}
63+
};
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @lc app=leetcode.cn id=105 lang=cpp
3+
*
4+
* [105] 从前序与中序遍历序列构造二叉树
5+
*
6+
* https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (61.35%)
10+
* Likes: 234
11+
* Dislikes: 0
12+
* Total Accepted: 23.3K
13+
* Total Submissions: 38K
14+
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
15+
*
16+
* 根据一棵树的前序遍历与中序遍历构造二叉树。
17+
*
18+
* 注意:
19+
* 你可以假设树中没有重复的元素。
20+
*
21+
* 例如,给出
22+
*
23+
* 前序遍历 preorder = [3,9,20,15,7]
24+
* 中序遍历 inorder = [9,3,15,20,7]
25+
*
26+
* 返回如下的二叉树:
27+
*
28+
* ⁠ 3
29+
* ⁠ / \
30+
* ⁠ 9 20
31+
* ⁠ / \
32+
* ⁠ 15 7
33+
*
34+
*/
35+
/**
36+
* Definition for a binary tree node.
37+
* struct TreeNode {
38+
* int val;
39+
* TreeNode *left;
40+
* TreeNode *right;
41+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
42+
* };
43+
*/
44+
class Solution {
45+
public:
46+
int pre=0;
47+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
48+
return subtree(preorder, inorder, 0, preorder.size()-1);
49+
}
50+
51+
TreeNode* subtree(vector<int>& preorder, vector<int>& inorder, int l, int r){
52+
if(l > r) return NULL;
53+
if(l == r) return new TreeNode(preorder[pre++]);
54+
55+
TreeNode* root = new TreeNode(preorder[pre++]);
56+
int id = find(inorder.begin()+l, inorder.begin()+l+r, preorder[pre-1]) - inorder.begin();
57+
root->left = subtree(preorder, inorder, l, id-1);
58+
root->right = subtree(preorder, inorder, id+1, r);
59+
return root;
60+
}
61+
};
62+

0 commit comments

Comments
 (0)