题目
题解
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {string[]}
*/
var binaryTreePaths = function(root) {
const patharr = [];
const dfs = (root, curpath) => {
if(root.left === null && root.right === null) {
curpath += root.val;
patharr.push(curpath);
return;
};
curpath += root.val + '->';
root.left && dfs(root.left, curpath);
root.right && dfs(root.right, curpath);
}
dfs(root, '')
return patharr;
};