35 lines
1011 B
Java
35 lines
1011 B
Java
# 28. 对称的二叉树
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb?tpId=13&tqId=11211&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
|
|
|
## 题目描述
|
|
|
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/0c12221f-729e-4c22-b0ba-0dfc909f8adf.jpg" width="300"/> </div><br>
|
|
|
|
## 解题思路
|
|
|
|
```java
|
|
boolean isSymmetrical(TreeNode pRoot) {
|
|
if (pRoot == null)
|
|
return true;
|
|
return isSymmetrical(pRoot.left, pRoot.right);
|
|
}
|
|
|
|
boolean isSymmetrical(TreeNode t1, TreeNode t2) {
|
|
if (t1 == null && t2 == null)
|
|
return true;
|
|
if (t1 == null || t2 == null)
|
|
return false;
|
|
if (t1.val != t2.val)
|
|
return false;
|
|
return isSymmetrical(t1.left, t2.right) && isSymmetrical(t1.right, t2.left);
|
|
}
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|