From 5512ccdb63370be27a387acab0c1ae366607b089 Mon Sep 17 00:00:00 2001 From: xll <18574553598@163.com> Date: Sat, 29 May 2021 10:44:58 +0800 Subject: [PATCH] =?UTF-8?q?0513=E4=BA=8C=E5=8F=89=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E5=B0=8F=E8=A7=92=E7=9A=84=E5=80=BCJavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 5c9613e5..97464e3d 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -298,6 +298,53 @@ class Solution: ``` Go: +JavaScript: +1. 递归版本 +```javascript +var findBottomLeftValue = function(root) { + //首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可 + let maxPath = 0,resNode = null; + // 1. 确定递归函数的函数参数 + const dfsTree = function(node,curPath){ + // 2. 确定递归函数终止条件 + if(node.left===null&&node.right===null){ + if(curPath>maxPath){ + maxPath = curPath; + resNode = node.val; + } + // return ; + } + node.left&&dfsTree(node.left,curPath+1); + node.right&&dfsTree(node.right,curPath+1); + } + dfsTree(root,1); + return resNode; +}; +``` +2. 层序遍历 +```javascript +var findBottomLeftValue = function(root) { + //考虑层序遍历 记录最后一行的第一个节点 + let queue = []; + if(root===null){ + return null; + } + queue.push(root); + let resNode; + while(queue.length){ + let length = queue.length; + for(let i=0; i