Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8df06af

Browse files
committed
solve 101.对称二叉树
1 parent 3f01c24 commit 8df06af

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

‎zh/101.对称二叉树.1.java‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=java
3+
*
4+
* [101] 对称二叉树
5+
*
6+
* https://leetcode-cn.com/problems/symmetric-tree/description/
7+
*
8+
* algorithms
9+
* Easy (51.08%)
10+
* Likes: 864
11+
* Dislikes: 0
12+
* Total Accepted: 163.8K
13+
* Total Submissions: 313.6K
14+
* Testcase Example: '[1,2,2,3,4,4,3]'
15+
*
16+
* 给定一个二叉树,检查它是否是镜像对称的。
17+
*
18+
*
19+
*
20+
* 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
21+
*
22+
* ⁠ 1
23+
* ⁠ / \
24+
* ⁠ 2 2
25+
* ⁠/ \ / \
26+
* 3 4 4 3
27+
*
28+
*
29+
*
30+
*
31+
* 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
32+
*
33+
* ⁠ 1
34+
* ⁠ / \
35+
* ⁠ 2 2
36+
* ⁠ \ \
37+
* ⁠ 3 3
38+
*
39+
*
40+
*
41+
*
42+
* 进阶:
43+
*
44+
* 你可以运用递归和迭代两种方法解决这个问题吗?
45+
*
46+
*/
47+
48+
// @lc code=start
49+
/**
50+
* Definition for a binary tree node.
51+
* public class TreeNode {
52+
* int val;
53+
* TreeNode left;
54+
* TreeNode right;
55+
* TreeNode(int x) { val = x; }
56+
* }
57+
*/
58+
class Solution {
59+
public boolean isSymmetric(TreeNode root) {
60+
return check(root, root);
61+
}
62+
63+
public boolean check(TreeNode node1, TreeNode node2) {
64+
if (node1 == null && node2 == null) {
65+
return true;
66+
}
67+
if (node1 == null || node2 == null) {
68+
return false;
69+
}
70+
return (node1.val == node2.val) &&
71+
check(node1.left, node2.right) &&
72+
check(node1.right, node2.left);
73+
}
74+
}
75+
// @lc code=end
76+

‎zh/101.对称二叉树.java‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=java
3+
*
4+
* [101] 对称二叉树
5+
*
6+
* https://leetcode-cn.com/problems/symmetric-tree/description/
7+
*
8+
* algorithms
9+
* Easy (51.08%)
10+
* Likes: 864
11+
* Dislikes: 0
12+
* Total Accepted: 163.8K
13+
* Total Submissions: 313.6K
14+
* Testcase Example: '[1,2,2,3,4,4,3]'
15+
*
16+
* 给定一个二叉树,检查它是否是镜像对称的。
17+
*
18+
*
19+
*
20+
* 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
21+
*
22+
* ⁠ 1
23+
* ⁠ / \
24+
* ⁠ 2 2
25+
* ⁠/ \ / \
26+
* 3 4 4 3
27+
*
28+
*
29+
*
30+
*
31+
* 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
32+
*
33+
* ⁠ 1
34+
* ⁠ / \
35+
* ⁠ 2 2
36+
* ⁠ \ \
37+
* ⁠ 3 3
38+
*
39+
*
40+
*
41+
*
42+
* 进阶:
43+
*
44+
* 你可以运用递归和迭代两种方法解决这个问题吗?
45+
*
46+
*/
47+
48+
// @lc code=start
49+
/**
50+
* Definition for a binary tree node.
51+
* public class TreeNode {
52+
* int val;
53+
* TreeNode left;
54+
* TreeNode right;
55+
* TreeNode(int x) { val = x; }
56+
* }
57+
*/
58+
class Solution {
59+
public boolean isSymmetric(TreeNode root) {
60+
Queue<TreeNode> queue = new LinkedList<>();
61+
queue.offer(root);
62+
queue.offer(root);
63+
64+
while (!queue.isEmpty()) {
65+
TreeNode node1 = queue.poll();
66+
TreeNode node2 = queue.poll();
67+
if (node1 == null && node2 == null) {
68+
continue;
69+
}
70+
if (node1 == null || node2 == null || node1.val != node2.val) {
71+
return false;
72+
}
73+
74+
queue.offer(node1.left);
75+
queue.offer(node2.right);
76+
77+
queue.offer(node1.right);
78+
queue.offer(node2.left);
79+
}
80+
81+
return true;
82+
}
83+
}
84+
// @lc code=end
85+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /