CS-Notes/notes/27. 二叉树的镜像.md

27 lines
638 B
Java
Raw Normal View History

2019-11-02 12:07:41 +08:00
# 27. 二叉树的镜像
2021-03-23 02:48:19 +08:00
[牛客网](https://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7?tpId=13&tqId=11171&tab=answerKey&from=cyc_github)
2019-11-02 12:07:41 +08:00
## 题目描述
2019-12-06 10:11:23 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/0c12221f-729e-4c22-b0ba-0dfc909f8adf.jpg" width="300"/> </div><br>
2019-11-02 12:07:41 +08:00
## 解题思路
```java
2021-03-23 02:48:19 +08:00
public TreeNode Mirror(TreeNode root) {
2019-11-02 12:07:41 +08:00
if (root == null)
2021-03-23 02:48:19 +08:00
return root;
2019-11-02 12:07:41 +08:00
swap(root);
Mirror(root.left);
Mirror(root.right);
2021-03-23 02:48:19 +08:00
return root;
2019-11-02 12:07:41 +08:00
}
private void swap(TreeNode root) {
TreeNode t = root.left;
root.left = root.right;
root.right = t;
}
```