-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-paths.js
More file actions
47 lines (39 loc) · 953 Bytes
/
binary-tree-paths.js
File metadata and controls
47 lines (39 loc) · 953 Bytes
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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {string[]}
*/
var binaryTreePaths = function(root) {
if (!root) return []
var result = []
var item = []
function me(node) {
if (!node.left && !node.right) {
item.push(node.val)
result.push(item.join('->'))
// 每次要删掉!!!
item.pop()
return
}
if (node.left) {
item.push(node.val)
me(node.left)
// 每次要删掉!!!
item.pop()
}
if (node.right) {
item.push(node.val)
me(node.right)
// 每次要删掉!!!
item.pop()
}
}
me(root)
return result
};