C++
Java
Python
Python3
C
C#
JavaScript
TypeScript
PHP
Swift
Kotlin
Dart
Go
Ruby
Scala
Rust
Racket
Erlang
Elixir
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
}
};
运行代码
提交
golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2023-10-21 19:31:23
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func upsideDownBinaryTree(root *TreeNode) *TreeNode {
var p, l, r *TreeNode
for root != nil {
l = root.Left //下一次循环
root.Left = r //上一个右节点
r = root.Right //下一次循环
root.Right = p //反转
p = root //下一次循环
root = l
}
return p
}
cpp 解法, 执行用时: 4 ms, 内存消耗: 9.7 MB, 提交时间: 2023-10-21 19:31:01
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
if (root == NULL || root->left == NULL) return root;
auto l = root->left;
auto r = root->right;
root->left = NULL;
root->right = NULL;
auto res = upsideDownBinaryTree(l);
l->left = r;
l->right = root;
return res;
}
};
java 解法, 执行用时: 0 ms, 内存消耗: 39.1 MB, 提交时间: 2023-10-21 19:30:29
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
TreeNode right = null, father = null;
while(root != null){
//为了继续遍历,先记录下原来的左子节点防止丢失
TreeNode left = root.left;
//当前节点的左子节点更新为父节点的右子节点
root.left = right;
//记录下当前节点的右子节点
right = root.right;
//当前节点的右子节点更新为原父节点
root.right = father;
//记录下当前节点作为下一个待遍历节点的父节点(新右子节点)
father = root;
root = left;
}
//最终root=null,father指向的是最终的根节点
return father;
}
}
java 解法, 执行用时: 0 ms, 内存消耗: 39.4 MB, 提交时间: 2023-10-21 19:30:04
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
TreeNode parent = null, parent_right = null;
while(root != null){
TreeNode root_left = root.left;
root.left = parent_right;
parent_right = root.right;
root.right = parent;
parent = root;
root = root_left;
}
return parent;
}
}
python3 解法, 执行用时: 44 ms, 内存消耗: 16.2 MB, 提交时间: 2023-10-21 19:29:51
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def upsideDownBinaryTree(self, root: TreeNode) -> TreeNode:
parent = parent_right = None
while root:
root_left = root.left
root.left = parent_right
parent_right = root.right
root.right = parent
parent = root
root = root_left
return parent