From b87fe3a4b3252ac7b6e2e08ff6e4224a073abce1 Mon Sep 17 00:00:00 2001 From: xll <18574553598@163.com> Date: Wed, 26 May 2021 10:36:11 +0800 Subject: [PATCH 1/3] =?UTF-8?q?0110.=E5=B9=B3=E8=A1=A1=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index dfc27344..1cd54849 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -142,7 +142,7 @@ int getDepth(TreeNode* node) 2. 明确终止条件 -递归的过程中依然是遇到空节点了为终止,返回0,表示当前节点为根节点的书高度为0 +递归的过程中依然是遇到空节点了为终止,返回0,表示当前节点为根节点的树高度为0 代码如下: @@ -534,7 +534,34 @@ func abs(a int)int{ return a } ``` - +JavaScript: +```javascript +var isBalanced = function(root) { + //还是用递归三部曲 + 后序遍历 左右中 当前左子树右子树高度相差大于1就返回-1 + // 1. 确定递归函数参数以及返回值 + const getDepth=function(node){ + // 2. 确定递归函数终止条件 + if(node===null){ + return 0; + } + // 3. 确定单层递归逻辑 + let leftDepth=getDepth(node.left);//左子树高度 + if(leftDepth===-1){ + return -1; + } + let rightDepth=getDepth(node.right);//右子树高度 + if(rightDepth===-1){ + return -1; + } + if(Math.abs(leftDepth-rightDepth)>1){ + return -1; + }else{ + return 1+Math.max(leftDepth,rightDepth); + } + } + return getDepth(root)===-1?false:true; +}; +``` ----------------------- From f51412e3d43b6bda140913d63f3575585d4ac981 Mon Sep 17 00:00:00 2001 From: xll <18574553598@163.com> Date: Wed, 26 May 2021 20:59:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?0257.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md=E2=80=98=20git=20commit?= =?UTF-8?q?=20-m=200257.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E8=B7=AF=E5=BE=84.=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 964bd7fd..fa9991de 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -351,6 +351,30 @@ class Solution: ``` Go: +JavaScript: +递归版本 +```javascript +var binaryTreePaths = function(root) { + //递归遍历+递归三部曲 + let res=[]; + //1. 确定递归函数 函数参数 + const getPath=function(node,curPath){ + //2. 确定终止条件,到叶子节点就终止 + if(node.left===null&&node.right===null){ + curPath+=node.val; + res.push(curPath); + return ; + } + //3. 确定单层递归逻辑 + curPath+=node.val+'->'; + node.left&&getPath(node.left,curPath); + node.right&&getPath(node.right,curPath); + } + getPath(root,''); + return res; +}; +``` + From ebf87c93bdf91429de1fca9fcf999e3ed24eb0a0 Mon Sep 17 00:00:00 2001 From: xll <18574553598@163.com> Date: Wed, 26 May 2021 21:07:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index fa9991de..fe7a2604 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -352,7 +352,7 @@ class Solution: Go: JavaScript: -递归版本 +1.递归版本 ```javascript var binaryTreePaths = function(root) { //递归遍历+递归三部曲