Skip to content

Commit 3ed2d9d

Browse files
committed
feat: add solutions to lc problem: No.0156
No.0156.Binary Tree Upside Down
1 parent d70614b commit 3ed2d9d

File tree

8 files changed

+286
-6
lines changed

8 files changed

+286
-6
lines changed

.github/workflows/contributors.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: wow-actions/contributors-list@v1
1414
name: Update contributors
1515
with:
16-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16+
GITHUB_TOKEN: ${{ secrets.ACTION_TOKEN }}
1717
svgPath: images/contributors.svg
1818
svgWidth: 890
1919
commitMessage: "chore: auto update contributors"

.github/workflows/starcharts.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
steps:
1313
- uses: MaoLongLong/actions-starcharts@main
1414
with:
15-
github_token: ${{ secrets.GITHUB_TOKEN }}
15+
github_token: ${{ secrets.ACTION_TOKEN }}
1616
svg_path: images/starcharts.svg
1717
commit_message: "chore: auto update starcharts"
1818
stars_change: "50"

solution/0100-0199/0156.Binary Tree Upside Down/README.md

+103-2
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,128 @@
4646

4747
<p>上面的二叉树则被序列化为 <code>[1,2,3,#,#,4,#,#,5]</code>.</p>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
5352

53+
若根节点为空,或者根节点左子树为空,直接返回根节点。
54+
55+
递归处理左子树,返回的根节点 newRoot,也就是二叉树上下翻转后的根节点。
56+
57+
然后处理根节点 root,根节点变成左子节点的右子节点,而根节点的右子节点变成左子节点的左子节点。
58+
59+
接着将根节点 root 的左右子节点置为空,最后返回 newRoot 即可。
60+
5461
<!-- tabs:start -->
5562

5663
### **Python3**
5764

5865
<!-- 这里可写当前语言的特殊实现逻辑 -->
5966

6067
```python
61-
68+
# Definition for a binary tree node.
69+
# class TreeNode:
70+
# def __init__(self, val=0, left=None, right=None):
71+
# self.val = val
72+
# self.left = left
73+
# self.right = right
74+
class Solution:
75+
def upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
76+
if root is None or root.left is None:
77+
return root
78+
new_root = self.upsideDownBinaryTree(root.left)
79+
root.left.right = root
80+
root.left.left = root.right
81+
root.left = None
82+
root.right = None
83+
return new_root
6284
```
6385

6486
### **Java**
6587

6688
<!-- 这里可写当前语言的特殊实现逻辑 -->
6789

6890
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
public TreeNode upsideDownBinaryTree(TreeNode root) {
108+
if (root == null || root.left == null) {
109+
return root;
110+
}
111+
TreeNode newRoot = upsideDownBinaryTree(root.left);
112+
root.left.right = root;
113+
root.left.left = root.right;
114+
root.left = null;
115+
root.right = null;
116+
return newRoot;
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for a binary tree node.
126+
* struct TreeNode {
127+
* int val;
128+
* TreeNode *left;
129+
* TreeNode *right;
130+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
131+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
132+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
133+
* };
134+
*/
135+
class Solution {
136+
public:
137+
TreeNode* upsideDownBinaryTree(TreeNode* root) {
138+
if (!root || !root->left) return root;
139+
TreeNode* newRoot = upsideDownBinaryTree(root->left);
140+
root->left->right = root;
141+
root->left->left = root->right;
142+
root->left = nullptr;
143+
root->right = nullptr;
144+
return newRoot;
145+
}
146+
};
147+
```
69148
149+
### **Go**
150+
151+
```go
152+
/**
153+
* Definition for a binary tree node.
154+
* type TreeNode struct {
155+
* Val int
156+
* Left *TreeNode
157+
* Right *TreeNode
158+
* }
159+
*/
160+
func upsideDownBinaryTree(root *TreeNode) *TreeNode {
161+
if root == nil || root.Left == nil {
162+
return root
163+
}
164+
newRoot := upsideDownBinaryTree(root.Left)
165+
root.Left.Right = root
166+
root.Left.Left = root.Right
167+
root.Left = nil
168+
root.Right = nil
169+
return newRoot
170+
}
70171
```
71172

72173
### **...**

solution/0100-0199/0156.Binary Tree Upside Down/README_EN.md

+95-2
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,114 @@
4949
<li><code>Every node has either 0 or 2 children.</code></li>
5050
</ul>
5151

52-
5352
## Solutions
5453

5554
<!-- tabs:start -->
5655

5756
### **Python3**
5857

5958
```python
60-
59+
# Definition for a binary tree node.
60+
# class TreeNode:
61+
# def __init__(self, val=0, left=None, right=None):
62+
# self.val = val
63+
# self.left = left
64+
# self.right = right
65+
class Solution:
66+
def upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
67+
if root is None or root.left is None:
68+
return root
69+
new_root = self.upsideDownBinaryTree(root.left)
70+
root.left.right = root
71+
root.left.left = root.right
72+
root.left = None
73+
root.right = None
74+
return new_root
6175
```
6276

6377
### **Java**
6478

6579
```java
80+
/**
81+
* Definition for a binary tree node.
82+
* public class TreeNode {
83+
* int val;
84+
* TreeNode left;
85+
* TreeNode right;
86+
* TreeNode() {}
87+
* TreeNode(int val) { this.val = val; }
88+
* TreeNode(int val, TreeNode left, TreeNode right) {
89+
* this.val = val;
90+
* this.left = left;
91+
* this.right = right;
92+
* }
93+
* }
94+
*/
95+
class Solution {
96+
public TreeNode upsideDownBinaryTree(TreeNode root) {
97+
if (root == null || root.left == null) {
98+
return root;
99+
}
100+
TreeNode newRoot = upsideDownBinaryTree(root.left);
101+
root.left.right = root;
102+
root.left.left = root.right;
103+
root.left = null;
104+
root.right = null;
105+
return newRoot;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
/**
114+
* Definition for a binary tree node.
115+
* struct TreeNode {
116+
* int val;
117+
* TreeNode *left;
118+
* TreeNode *right;
119+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
120+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
121+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
122+
* };
123+
*/
124+
class Solution {
125+
public:
126+
TreeNode* upsideDownBinaryTree(TreeNode* root) {
127+
if (!root || !root->left) return root;
128+
TreeNode* newRoot = upsideDownBinaryTree(root->left);
129+
root->left->right = root;
130+
root->left->left = root->right;
131+
root->left = nullptr;
132+
root->right = nullptr;
133+
return newRoot;
134+
}
135+
};
136+
```
66137
138+
### **Go**
139+
140+
```go
141+
/**
142+
* Definition for a binary tree node.
143+
* type TreeNode struct {
144+
* Val int
145+
* Left *TreeNode
146+
* Right *TreeNode
147+
* }
148+
*/
149+
func upsideDownBinaryTree(root *TreeNode) *TreeNode {
150+
if root == nil || root.Left == nil {
151+
return root
152+
}
153+
newRoot := upsideDownBinaryTree(root.Left)
154+
root.Left.Right = root
155+
root.Left.Left = root.Right
156+
root.Left = nil
157+
root.Right = nil
158+
return newRoot
159+
}
67160
```
68161

69162
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* upsideDownBinaryTree(TreeNode* root) {
15+
if (!root || !root->left) return root;
16+
TreeNode* newRoot = upsideDownBinaryTree(root->left);
17+
root->left->right = root;
18+
root->left->left = root->right;
19+
root->left = nullptr;
20+
root->right = nullptr;
21+
return newRoot;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func upsideDownBinaryTree(root *TreeNode) *TreeNode {
10+
if root == nil || root.Left == nil {
11+
return root
12+
}
13+
newRoot := upsideDownBinaryTree(root.Left)
14+
root.Left.Right = root
15+
root.Left.Left = root.Right
16+
root.Left = nil
17+
root.Right = nil
18+
return newRoot
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode upsideDownBinaryTree(TreeNode root) {
18+
if (root == null || root.left == null) {
19+
return root;
20+
}
21+
TreeNode newRoot = upsideDownBinaryTree(root.left);
22+
root.left.right = root;
23+
root.left.left = root.right;
24+
root.left = null;
25+
root.right = null;
26+
return newRoot;
27+
}
28+
}
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, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9+
if root is None or root.left is None:
10+
return root
11+
new_root = self.upsideDownBinaryTree(root.left)
12+
root.left.right = root
13+
root.left.left = root.right
14+
root.left = None
15+
root.right = None
16+
return new_root

0 commit comments

Comments
 (0)