Skip to content

Commit f51412e

Browse files
committed
0257.二叉树的所有路径.md‘
git commit -m 0257.二叉树的所有路径.’
1 parent 3fa5571 commit f51412e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

problems/0257.二叉树的所有路径.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ class Solution:
351351
```
352352
Go:
353353

354+
JavaScript:
355+
递归版本
356+
```javascript
357+
var binaryTreePaths = function(root) {
358+
//递归遍历+递归三部曲
359+
let res=[];
360+
//1. 确定递归函数 函数参数
361+
const getPath=function(node,curPath){
362+
//2. 确定终止条件,到叶子节点就终止
363+
if(node.left===null&&node.right===null){
364+
curPath+=node.val;
365+
res.push(curPath);
366+
return ;
367+
}
368+
//3. 确定单层递归逻辑
369+
curPath+=node.val+'->';
370+
node.left&&getPath(node.left,curPath);
371+
node.right&&getPath(node.right,curPath);
372+
}
373+
getPath(root,'');
374+
return res;
375+
};
376+
```
377+
354378

355379

356380

0 commit comments

Comments
 (0)