Skip to content

Commit 9bf776c

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer题解》:面试题27. 二叉树的镜像
1 parent 98ff7a9 commit 9bf776c

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

README_CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
## 项目介绍
1818
本项目包含 [LeetCode](https://leetcode-cn.com/problemset/all/)[《剑指 Offer(第 2 版)》](https://leetcode-cn.com/problemset/lcof/)[《程序员面试金典(第 6 版)》](https://leetcode-cn.com/problemset/lcci/)等题目的相关题解。所有题解均由多种编程语言实现,包括但不限于:Java、Python、C++、JavaScript、C#、Go,日常更新。
1919

20-
[English version here.](./README.md)
20+
[English Version](./README.md)
2121

2222
## 题解
2323
- [LeetCode](./solution/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [面试题27. 二叉树的镜像](https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/)
2+
3+
## 题目描述
4+
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
5+
6+
例如输入:
7+
8+
```
9+
     4
10+
   /   \
11+
  2     7
12+
 / \   / \
13+
1   3 6   9
14+
```
15+
16+
镜像输出:
17+
18+
```
19+
     4
20+
   /   \
21+
  7     2
22+
 / \   / \
23+
9   6 3   1
24+
```
25+
 
26+
27+
**示例 1:**
28+
29+
```
30+
输入:root = [4,2,7,1,3,6,9]
31+
输出:[4,7,2,9,6,3,1]
32+
```
33+
34+
**限制:**
35+
36+
- `0 <= 节点个数 <= 1000`
37+
38+
## 解法
39+
### Python3
40+
```python
41+
# Definition for a binary tree node.
42+
# class TreeNode:
43+
# def __init__(self, x):
44+
# self.val = x
45+
# self.left = None
46+
# self.right = None
47+
48+
class Solution:
49+
def mirrorTree(self, root: TreeNode) -> TreeNode:
50+
if root is None or (root.left is None and root.right is None):
51+
return root
52+
53+
self.mirrorTree(root.left)
54+
self.mirrorTree(root.right)
55+
root.left, root.right = root.right, root.left
56+
return root
57+
```
58+
59+
### Java
60+
```java
61+
/**
62+
* Definition for a binary tree node.
63+
* public class TreeNode {
64+
* int val;
65+
* TreeNode left;
66+
* TreeNode right;
67+
* TreeNode(int x) { val = x; }
68+
* }
69+
*/
70+
class Solution {
71+
public TreeNode mirrorTree(TreeNode root) {
72+
if (root == null || (root.left == null && root.right == null)) {
73+
return root;
74+
}
75+
mirrorTree(root.left);
76+
mirrorTree(root.right);
77+
TreeNode t = root.left;
78+
root.left = root.right;
79+
root.right = t;
80+
return root;
81+
}
82+
}
83+
```
84+
85+
### ...
86+
```
87+
88+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode mirrorTree(TreeNode root) {
12+
if (root == null || (root.left == null && root.right == null)) {
13+
return root;
14+
}
15+
mirrorTree(root.left);
16+
mirrorTree(root.right);
17+
TreeNode t = root.left;
18+
root.left = root.right;
19+
root.right = t;
20+
return root;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def mirrorTree(self, root: TreeNode) -> TreeNode:
10+
if root is None or (root.left is None and root.right is None):
11+
return root
12+
13+
self.mirrorTree(root.left)
14+
self.mirrorTree(root.right)
15+
root.left, root.right = root.right, root.left
16+
return root

0 commit comments

Comments
 (0)