C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
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:
void recoverTree(TreeNode* root) {
}
};
运行代码
提交
golang 解法, 执行用时: 16 ms, 内存消耗: 5.5 MB, 提交时间: 2022-11-24 10:59:03
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func recoverTree(root *TreeNode) {
var x, y, pred, predecessor *TreeNode
for root != nil {
if root.Left != nil {
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
predecessor = root.Left
for predecessor.Right != nil && predecessor.Right != root {
predecessor = predecessor.Right
}
// 让 predecessor 的右指针指向 root,继续遍历左子树
if predecessor.Right == nil {
predecessor.Right = root
root = root.Left
} else { // 说明左子树已经访问完了,我们需要断开链接
if pred != nil && root.Val < pred.Val {
y = root
if x == nil {
x = pred
}
}
pred = root
predecessor.Right = nil
root = root.Right
}
} else { // 如果没有左孩子,则直接访问右孩子
if pred != nil && root.Val < pred.Val {
y = root
if x == nil {
x = pred
}
}
pred = root
root = root.Right
}
}
x.Val, y.Val = y.Val, x.Val
}
golang 解法, 执行用时: 12 ms, 内存消耗: 5.8 MB, 提交时间: 2022-11-24 10:58:37
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func recoverTree(root *TreeNode) {
stack := []*TreeNode{}
var x, y, pred *TreeNode
for len(stack) > 0 || root != nil {
for root != nil {
stack = append(stack, root)
root = root.Left
}
root = stack[len(stack)-1]
stack = stack[:len(stack)-1]
if pred != nil && root.Val < pred.Val {
y = root
if x == nil {
x = pred
} else {
break
}
}
pred = root
root = root.Right
}
x.Val, y.Val = y.Val, x.Val
}
golang 解法, 执行用时: 12 ms, 内存消耗: 6.2 MB, 提交时间: 2022-11-24 10:58:00
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func recoverTree(root *TreeNode) {
nums := []int{}
var inorder func(node *TreeNode)
inorder = func(node *TreeNode) {
if node == nil {
return
}
inorder(node.Left)
nums = append(nums, node.Val)
inorder(node.Right)
}
inorder(root)
x, y := findTwoSwapped(nums)
recover(root, 2, x, y)
}
func findTwoSwapped(nums []int) (int, int) {
index1, index2 := -1, -1
for i := 0; i < len(nums) - 1; i++ {
if nums[i + 1] < nums[i] {
index2 = i + 1
if index1 == -1 {
index1 = i
} else {
break
}
}
}
x, y := nums[index1], nums[index2]
return x, y
}
func recover(root *TreeNode, count, x, y int) {
if root == nil {
return
}
if root.Val == x || root.Val == y {
if root.Val == x {
root.Val = y
} else {
root.Val = x
}
count--
if count == 0 {
return
}
}
recover(root.Right, count, x, y)
recover(root.Left, count, x, y)
}