Skip to content

Commit

Permalink
添加(二叉树中递归带着回溯.md):增加二叉树的不同路径的typescript版本
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofei-2020 committed Feb 8, 2022
1 parent 47818c9 commit 49a1a42
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions problems/二叉树中递归带着回溯.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,27 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
> 二叉树的不同路径
```typescript
function binaryTreePaths(root: TreeNode | null): string[] {
function recur(node: TreeNode, nodeSeqArr: number[], resArr: string[]): void {
nodeSeqArr.push(node.val);
if (node.left === null && node.right === null) {
resArr.push(nodeSeqArr.join('->'));
}
if (node.left !== null) {
recur(node.left, nodeSeqArr, resArr);
nodeSeqArr.pop();
}
if (node.right !== null) {
recur(node.right, nodeSeqArr, resArr);
nodeSeqArr.pop();
}
}
let nodeSeqArr: number[] = [];
let resArr: string[] = [];
if (root === null) return resArr;
recur(root, nodeSeqArr, resArr);
return resArr;
};
```


Expand Down

0 comments on commit 49a1a42

Please sign in to comment.