From 2e644523623fbe50103e4bd073b2803816182197 Mon Sep 17 00:00:00 2001 From: Om Vyas Date: Sat, 29 Jan 2022 12:18:04 +0530 Subject: [PATCH 1/2] Recover Binary Search Tree - 99 --- .../README_EN.md | 45 +++++++++++++++++++ .../Solution.js | 41 +++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 solution/0000-0099/0099.Recover Binary Search Tree/Solution.js diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md b/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md index 1b7ba127f3b31..3ad0dc69cacce 100644 --- a/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md +++ b/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md @@ -243,6 +243,51 @@ public class Solution { } ``` +### **Javascript** +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {void} Do not return anything, modify root in-place instead. + */ +const recoverTree = root => { + const data = { + prev: null, + first: null, + second: null + }; + let tmp = 0; + + helper(root, data); + + tmp = data.first.val; + data.first.val = data.second.val; + data.second.val = tmp; +}; + +const helper = (root, data) => { + if (!root) return; + + helper(root.left, data); + + if (data.prev && data.prev.val >= root.val) { + if (!data.first) data.first = data.prev; + data.second = root; + } + + data.prev = root; + + helper(root.right, data); +}; +``` + ### **...** ``` diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/Solution.js b/solution/0000-0099/0099.Recover Binary Search Tree/Solution.js new file mode 100644 index 0000000000000..869b3ad8c2a3d --- /dev/null +++ b/solution/0000-0099/0099.Recover Binary Search Tree/Solution.js @@ -0,0 +1,41 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {void} Do not return anything, modify root in-place instead. + */ +const recoverTree = root => { + const data = { + prev: null, + first: null, + second: null + }; + let tmp = 0; + + helper(root, data); + + tmp = data.first.val; + data.first.val = data.second.val; + data.second.val = tmp; +}; + +const helper = (root, data) => { + if (!root) return; + + helper(root.left, data); + + if (data.prev && data.prev.val >= root.val) { + if (!data.first) data.first = data.prev; + data.second = root; + } + + data.prev = root; + + helper(root.right, data); +}; \ No newline at end of file From 2a7a0270d2c38cf3109186e46da67de2600de730 Mon Sep 17 00:00:00 2001 From: Om Vyas Date: Sat, 29 Jan 2022 13:06:30 +0530 Subject: [PATCH 2/2] README updated --- .../0099.Recover Binary Search Tree/README.md | 46 +++++++++++++++++++ .../README_EN.md | 3 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/README.md b/solution/0000-0099/0099.Recover Binary Search Tree/README.md index de42d2ebc7b22..bd93de3d54ab2 100644 --- a/solution/0000-0099/0099.Recover Binary Search Tree/README.md +++ b/solution/0000-0099/0099.Recover Binary Search Tree/README.md @@ -252,6 +252,52 @@ public class Solution { } ``` +### **JavaScript** + +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {void} Do not return anything, modify root in-place instead. + */ +const recoverTree = root => { + const data = { + prev: null, + first: null, + second: null + }; + let tmp = 0; + + helper(root, data); + + tmp = data.first.val; + data.first.val = data.second.val; + data.second.val = tmp; +}; + +const helper = (root, data) => { + if (!root) return; + + helper(root.left, data); + + if (data.prev && data.prev.val >= root.val) { + if (!data.first) data.first = data.prev; + data.second = root; + } + + data.prev = root; + + helper(root.right, data); +}; +``` + ### **...** ``` diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md b/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md index 3ad0dc69cacce..c85bb1ef7aca8 100644 --- a/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md +++ b/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md @@ -243,7 +243,8 @@ public class Solution { } ``` -### **Javascript** +### **JavaScript** + ```javascript /** * Definition for a binary tree node.