-
Notifications
You must be signed in to change notification settings - Fork 25
/
0257-binary-tree-paths.js
59 lines (45 loc) · 1.27 KB
/
0257-binary-tree-paths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 257. Binary Tree Paths
// Easy 39%
// Given a binary tree, return all root-to-leaf paths.
// For example, given the following binary tree:
// 1
// / \
// 2 3
// \
// 5
// All root-to-leaf paths are:
// ["1->2->5", "1->3"]
// Credits:Special thanks to @jianchao.li.fighter for adding this problem and
// creating all test cases.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {string[]}
*/
const binaryTreePaths = function(root) {
const array = []
function traversal(root, string) {
string += (string === '' ? '' : '->') + root.val
if (root.left) traversal(root.left, string, array)
if (root.right) traversal(root.right, string, array)
if (!root.left && !root.right) array.push(string)
}
if (root) traversal(root, '')
return array
}
const TreeNode = require('../structs/TreeNode')
;[
[1,2,3,null,5], // ['1->2->5', '1->3']
].forEach((array) => {
console.log(binaryTreePaths(TreeNode.from(array)))
})
// Solution:
// 递归一遍树,带上字符参数,每遍历一个节点,添加一个值。
// 若是叶子节点,再加完值之后,将字符串添加到数组中。
// Submission Result: Accepted