Appearance
257二叉树所有路径
code
javascript
/**
* @param {TreeNode} root
* @return {string[]}
*/
var binaryTreePaths = function (root) {
let result = [];
function getRoute(root, temp) {
if (root === null) {
return;
}
if (root.left === null && root.right === null) {
if ((temp === "")) {
temp = temp + String(root.val);
result.push(temp);
} else {
temp = temp + "->" + String(root.val);
result.push(temp);
}
return;
}
if ((temp === "")) {
temp = temp + String(root.val);
} else {
temp = temp + "->" + String(root.val);
}
getRoute(root.left, temp);
getRoute(root.right, temp);
}
getRoute(root, '');
return result;
};总结
- 赋值给赋傻了,折腾半天
- 总体我觉得倒还好,判定是不是叶子,流程啥的,都挺清晰的,偶尔可以看一下,比前中后序遍历强