/**
* 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 paths = [];
function findAllPaths(node, path) {
if (!node) return;
const { val, left, right } = node;
path.push(val);
!left && !right && paths.push(path.join("->"));
findAllPaths(node.left, path);
findAllPaths(node.right, path);
path.pop();
}
findAllPaths(root, []);
return paths;
};
时间复杂度和空间复杂度是多少?