C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
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* reverseOddLevels(TreeNode* root) {
}
};
运行代码
提交
java 解法, 执行用时: 8 ms, 内存消耗: 46.1 MB, 提交时间: 2023-12-15 00:05:03
/**
* 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;
* }
* }
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class Solution {
public TreeNode reverseOddLevels(TreeNode root) {
if (root == null) {
return null;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
if (level % 2 == 1) {
//反转该层的每一个节点的值
List<TreeNode> nodes = new ArrayList<>(queue);
for (int i = 0; i < nodes.size() / 2; i++) {
TreeNode treeNode = nodes.get(i);
TreeNode treeNode1 = nodes.get(nodes.size() - 1 - i);
int temp = treeNode.val;
treeNode.val = treeNode1.val;
treeNode1.val = temp;
}
}
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
level++;
}
return root;
}
}
java 解法, 执行用时: 0 ms, 内存消耗: 47.2 MB, 提交时间: 2023-12-15 00:04:21
/**
* 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 reverseOddLevels(TreeNode root) {
dfs(root.left, root.right, 1);
return root;
}
void dfs(TreeNode l, TreeNode r, int level) {
if (l == null) {
return;
}
if (level % 2 != 0) {
int val = r.val;
r.val = l.val;
l.val = val;
}
dfs(l.left, r.right, level + 1);
dfs(l.right, r.left, level + 1);
}
}
golang 解法, 执行用时: 68 ms, 内存消耗: 8 MB, 提交时间: 2022-11-17 16:04:58
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func reverseOddLevels(root *TreeNode) *TreeNode {
q := []*TreeNode{root}
for level := 0; q[0].Left != nil; level ^= 1 {
next := make([]*TreeNode, 0, len(q)*2)
for _, node := range q {
next = append(next, node.Left, node.Right)
}
q = next
if level == 0 {
for i, n := 0, len(q); i < n/2; i++ {
x, y := q[i], q[n-1-i]
x.Val, y.Val = y.Val, x.Val
}
}
}
return root
}
golang 解法, 执行用时: 60 ms, 内存消耗: 7.7 MB, 提交时间: 2022-11-17 16:04:43
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func dfs(node1 *TreeNode, node2 *TreeNode, isOddLevel bool) {
if node1 == nil {
return
}
if isOddLevel {
node1.Val, node2.Val = node2.Val, node1.Val
}
dfs(node1.Left, node2.Right, !isOddLevel)
dfs(node1.Right, node2.Left, !isOddLevel)
}
func reverseOddLevels(root *TreeNode) *TreeNode {
dfs(root.Left, root.Right, true)
return root
}
python3 解法, 执行用时: 304 ms, 内存消耗: 20.6 MB, 提交时间: 2022-11-17 16:04:17
# 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 reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(node1: Optional[TreeNode], node2: Optional[TreeNode], is_odd_level: bool) -> None:
if node1 is None: return
if is_odd_level: node1.val, node2.val = node2.val, node1.val
dfs(node1.left, node2.right, not is_odd_level)
dfs(node1.right, node2.left, not is_odd_level)
dfs(root.left, root.right, True)
return root
python3 解法, 执行用时: 260 ms, 内存消耗: 20.7 MB, 提交时间: 2022-11-17 16:03:05
# 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
'''BFS 这棵树,对于奇数层,直接交换层里面的所有元素值(交换的是元素值,不是节点)'''
class Solution:
def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
q, level = [root], 0
while q[0].left:
q = list(chain.from_iterable((node.left, node.right) for node in q))
if level == 0:
for i in range(len(q) // 2):
x, y = q[i], q[len(q) - 1 - i]
x.val, y.val = y.val, x.val
level ^= 1
return root