Skip to content

Commit

Permalink
update 后序遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
wangenze267 committed Mar 19, 2024
1 parent 50b12a1 commit ab71130
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,32 @@ var preorderTraversal = function(root) {
dfs(root)
return arr
};
```

### 二叉树的后序遍历

题目:[144.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/description/)

后序遍历的口诀:**左、右、根**

了解前序遍历后,这个应该很简单了,我们直接上代码:

```js
/**
* @param {TreeNode} root
* @return {number[]}
*/
var postorderTraversal = function(root) {
const arr = []
const dfs = (root) => {
if(root === null) return
if(root.val !== null) {
dfs(root.left)
dfs(root.right)
arr.push(root.val)
}
}
dfs(root)
return arr
};
```

0 comments on commit ab71130

Please sign in to comment.