From fa601e179aab6b17b31a9a88d2af0cf62fe1464e Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Mon, 21 Jun 2021 15:46:42 +0800 Subject: [PATCH 1/6] =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index be6bb425..2ff093a3 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -370,6 +370,36 @@ func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode { } ``` +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} root1 + * @param {TreeNode} root2 + * @return {TreeNode} + */ +var mergeTrees = function (root1, root2) { + const preOrder = (root1, root2) => { + if (!root1) + return root2 + if (!root2) + return root1; + root1.val += root2.val; + root1.left = preOrder(root1.left, root2.left); + root1.right = preOrder(root1.right, root2.right); + return root1; + } + return preOrder(root1, root2); +}; +``` ----------------------- From 75ef824eb6025cf9555ecdea0fa653f0ec88b7cb Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Mon, 21 Jun 2021 15:49:05 +0800 Subject: [PATCH 2/6] =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E3=80=8A=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=90=9C=E7=B4=A2=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 16b21f26..d6899ac5 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -290,7 +290,64 @@ func searchBST(root *TreeNode, val int) *TreeNode { } ``` +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 + * @param {number} val + * @return {TreeNode} + */ +var searchBST = function (root, val) { + if (!root || root.val === val) { + return root; + } + if (root.val > val) + return searchBST(root.left, val); + if (root.val < val) + return searchBST(root.right, val); + return null; +}; +``` + +> 迭代 + +```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 + * @param {number} val + * @return {TreeNode} + */ +var searchBST = function (root, val) { + while (root !== null) { + if (root.val > val) + root = root.left; + else if (root.val < val) + root = root.right; + else + return root; + } + return root; +}; +``` From 19b7541a7ec06e19638ed680c96cc99beea57f18 Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Mon, 21 Jun 2021 15:51:11 +0800 Subject: [PATCH 3/6] =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E3=80=8A=E9=AA=8C=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 69 ++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index eb877abb..e38c5ade 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -377,6 +377,75 @@ func isBST(root *TreeNode, min, max int) bool { } ``` +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 {boolean} + */ +var isValidBST = function (root) { + let arr = []; + const buildArr = (root) => { + if (root) { + buildArr(root.left); + arr.push(root.val); + buildArr(root.right); + } + } + buildArr(root); + for (let i = 1; i < arr.length; ++i) { + if (arr[i] <= arr[i - 1]) + return false; + } + return true; +}; +``` + +> 递归中解决 + +```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 {boolean} + */ +let pre = null; +var isValidBST = function (root) { + let pre = null; + const inOrder = (root) => { + if (root === null) + return true; + let left = inOrder(root.left); + + if (pre !== null && pre.val >= root.val) + return false; + pre = root; + + let right = inOrder(root.right); + return left && right; + } + return inOrder(root); +}; +``` + ----------------------- From 8969d0fd654da6881238e59aafee6f7613ad2811 Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Mon, 21 Jun 2021 21:32:02 +0800 Subject: [PATCH 4/6] =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E3=80=8A=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9=E5=B7=AE=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 0bbc4908..8fa756bc 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -256,6 +256,39 @@ func findMIn(root *TreeNode,res *[]int){ } ``` +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 {number} + */ +var getMinimumDifference = function (root) { + let arr = []; + const buildArr = (root) => { + if (root) { + buildArr(root.left); + arr.push(root.val); + buildArr(root.right); + } + } + buildArr(root); + let diff = arr[arr.length - 1]; + for (let i = 1; i < arr.length; ++i) { + if (diff > arr[i] - arr[i - 1]) + diff = arr[i] - arr[i - 1]; + } + return diff; +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 4f66b8679ca619b6e3087934d4934f6538ec6769 Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Mon, 21 Jun 2021 21:33:43 +0800 Subject: [PATCH 5/6] =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E3=80=8A=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E4=BC=97=E6=95=B0=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0501.二叉搜索树中的众数.md | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 3ca7d892..1831a7a5 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -523,6 +523,55 @@ func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计 } ``` +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 {number[]} + */ +var findMode = function (root) { + let maxCount = 0; + let curCount = 0; + let pre = null; + let res = []; + const inOrder = (root) => { + if (root === null) + return; + inOrder(root.left); + + if (pre === null) + curCount = 1; + else if (pre.val === root.val) + curCount++; + else + curCount = 1; + pre = root; + + if (curCount === maxCount) + res.push(root.val); + + if (curCount > maxCount) { + maxCount = curCount; + res.splice(0, res.length); + res.push(root.val); + } + + inOrder(root.right); + return; + } + inOrder(root); + return res; +}; +``` ----------------------- From b33da0c412e49d67610bc7d6016c8e51541da637 Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Mon, 21 Jun 2021 21:35:19 +0800 Subject: [PATCH 6/6] =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E3=80=8A=E4=BA=8C=E5=8F=89=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=A5=96=E5=85=88=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0236.二叉树的最近公共祖先.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 2f7aa6c3..2413af1d 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -311,7 +311,34 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { } ``` +JavaScript版本 +```javascript +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + if(root === p || root === q || root === null) + return root; + let left = lowestCommonAncestor(root.left, p , q); + let right = lowestCommonAncestor(root.right, p, q); + if(left && right) + return root; + if(!left) + return right; + return left; +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)