-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101-对称二叉树.java
More file actions
42 lines (35 loc) · 903 Bytes
/
101-对称二叉树.java
File metadata and controls
42 lines (35 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 给定一个二叉树,检查它是否是镜像对称的。
// 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
// 1
// / \
// 2 2
// / \ / \
// 3 4 4 3
// 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
// 1
// / \
// 2 2
// \ \
// 3 3
//
// Definition for a binary tree node.
// public class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int x) { val = x; }
// }
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return symmetric(root.left, root.right);
}
private boolean symmetric(TreeNode a, TreeNode b) {
if (a == null || b == null) {
return a == null && b == null;
}
return a.val == b.val && symmetric(a.left, b.right) && symmetric(a.right, b.left);
}
}