LeetCode 99. Recover Binary Search Tree | JSer - Front-End Interview questions

preview_player
Показать описание
LeetCode 99. Recover Binary Search Tree
If met with tree problems, think about recursion and traversal methods.

Hi I'm a JavaScript engineer who is not good at algorithms,
and currently practicing leetCode & BFE.dev. If you are interested, maybe we can learn together.
Рекомендации по теме
Комментарии
Автор

Hi @JSer,
I have one doubt in your code
var recoverTree = function(root) {

let prev = null
let first = null
let second = null


const walk = (node) => {
if (node === null) return
if (node.left) {
walk(node.left)
}

if (prev?.val > node.val) {
if (first === null) {
first = prev
}

second = node
}

prev = node

if (node.right) {
walk(node.right)
}
}

walk(root);

[first.val, second.val] = [second.val, first.val]
};

In this line
[first.val, second.val] = [second.val, first.val]
how the first and second variable is changing the nodes in root

suriyasuriyan
welcome to shbcf.ru