|
46 | 46 |
|
47 | 47 | <p>上面的二叉树则被序列化为 <code>[1,2,3,#,#,4,#,#,5]</code>.</p>
|
48 | 48 |
|
49 |
| - |
50 | 49 | ## 解法
|
51 | 50 |
|
52 | 51 | <!-- 这里可写通用的实现逻辑 -->
|
53 | 52 |
|
| 53 | +若根节点为空,或者根节点左子树为空,直接返回根节点。 |
| 54 | + |
| 55 | +递归处理左子树,返回的根节点 newRoot,也就是二叉树上下翻转后的根节点。 |
| 56 | + |
| 57 | +然后处理根节点 root,根节点变成左子节点的右子节点,而根节点的右子节点变成左子节点的左子节点。 |
| 58 | + |
| 59 | +接着将根节点 root 的左右子节点置为空,最后返回 newRoot 即可。 |
| 60 | + |
54 | 61 | <!-- tabs:start -->
|
55 | 62 |
|
56 | 63 | ### **Python3**
|
57 | 64 |
|
58 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
59 | 66 |
|
60 | 67 | ```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 |
62 | 84 | ```
|
63 | 85 |
|
64 | 86 | ### **Java**
|
65 | 87 |
|
66 | 88 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
67 | 89 |
|
68 | 90 | ```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 | +``` |
69 | 148 |
|
| 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 | +} |
70 | 171 | ```
|
71 | 172 |
|
72 | 173 | ### **...**
|
|
0 commit comments