Skip to content

Commit b0c5b46

Browse files
committed
feat: add 101
1 parent 827002b commit b0c5b46

File tree

8 files changed

+400
-91
lines changed

8 files changed

+400
-91
lines changed

note/001/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Solution {
3838
}
3939
```
4040

41-
4241
## 思路1
4342

4443
利用HashMap作为存储,键为目标值减去当前元素值,索引为值,比如`i = 0`时,此时首先要判断`nums[0] = 2`是否在map中,如果不存在,那么插入键值对`key = 9 - 2 = 7, value = 0`,之后当`i = 1`时,此时判断`nums[1] = 7`已存在于map中,那么取出该`value = 0`作为第一个返回值,当前`i`作为第二个返回值,具体代码如下所示。

note/100/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Two binary trees are considered equal if they are structurally identical and the
1111

1212
## 思路
1313

14-
题意是
14+
题意是比较两棵二叉树是否相同,那么我们就深搜比较各个节点即可。
1515

1616
``` java
1717
/**

note/101/README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [Symmetric Tree][title]
2+
3+
## Description
4+
5+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
6+
7+
For example, this binary tree `[1,2,2,3,4,4,3]` is symmetric:
8+
9+
```
10+
1
11+
/ \
12+
2 2
13+
/ \ / \
14+
3 4 4 3
15+
16+
```
17+
18+
But the following `[1,2,2,null,3,null,3]` is not:
19+
20+
```
21+
1
22+
/ \
23+
2 2
24+
\ \
25+
3 3
26+
27+
```
28+
29+
**Note:**
30+
Bonus points if you could solve it both recursively and iteratively.
31+
32+
**Tags:** Tree, Depth-first Search, Breadth-first Search
33+
34+
35+
## 思路0
36+
37+
题意是判断一棵二叉树是否左右对称,首先想到的是深搜,比较根结点的左右两棵子树是否对称,如果左右子树的值相同,那么再分别对左子树的左节点和右子树的右节点,左子树的右节点和右子树的左节点做比较即可。
38+
39+
``` java
40+
/**
41+
* Definition for a binary tree node.
42+
* public class TreeNode {
43+
* int val;
44+
* TreeNode left;
45+
* TreeNode right;
46+
* TreeNode(int x) { val = x; }
47+
* }
48+
*/
49+
class Solution {
50+
public boolean isSymmetric(TreeNode root) {
51+
return root == null || isSymmetricHelper(root.left, root.right);
52+
}
53+
54+
public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
55+
if (left == null || right == null) return left == right;
56+
if (left.val != right.val) return false;
57+
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
58+
}
59+
}
60+
```
61+
62+
## 思路1
63+
64+
第二种思路就是宽搜了,宽搜肯定要用到队列,Java中可用`LinkedList`替代,也是要做到左子树的左节点和右子树的右节点,左子树的右节点和右子树的左节点做比较即可。
65+
66+
``` java
67+
/**
68+
* Definition for a binary tree node.
69+
* public class TreeNode {
70+
* int val;
71+
* TreeNode left;
72+
* TreeNode right;
73+
* TreeNode(int x) { val = x; }
74+
* }
75+
*/
76+
class Solution {
77+
public boolean isSymmetric(TreeNode root) {
78+
if (root == null) return true;
79+
LinkedList<TreeNode> q = new LinkedList<>();
80+
q.add(root.left);
81+
q.add(root.right);
82+
TreeNode left, right;
83+
while (q.size() > 1) {
84+
left = q.pop();
85+
right = q.pop();
86+
if (left == null && right == null) continue;
87+
if (left == null || right == null) return false;
88+
if (left.val != right.val) return false;
89+
q.add(left.left);
90+
q.add(right.right);
91+
q.add(left.right);
92+
q.add(right.left);
93+
}
94+
return true;
95+
}
96+
}
97+
```
98+
99+
## 结语
100+
101+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
102+
103+
104+
105+
[title]: https://leetcode.com/problems/symmetric-tree
106+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

0 commit comments

Comments
 (0)