---
# System prepended metadata

title: \#145 Binary Tree Postorder Traversal
tags: [binary tree, leetcode, leetcode-easy]

---

# \#145 Binary Tree Postorder Traversal
## *遍歷二元樹(root.left->root.right->root*
## Log
 - build 20210515 by syhuang

## 初見2
 - 非遞迴版實作
```javascript=
var postorderTraversal = function(root) {
    var result = [];
    var t = [];//暫存p
    var p = root;
    while(p || t.length > 0){
        if(p){
            t.push(p);
            result.unshift(p.val);
            p = p.right;
        }else{//已經到了leaf(沒有左節點和右節點), 開始回溯左節點
            p = t.pop().left;
        }
    }
    return result;
};
```
## 初見
 - 遞迴版實作
```javascript=
var result = [];
var postorderTraversal = function(root) {
    if(!root) return [];
    return result.concat(
        postorderTraversal(root.left),
        postorderTraversal(root.right),
        root.val
    );
};
```
## 備註
## 參考
 - [參考解](https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45551/Preorder-Inorder-and-Postorder-Iteratively-Summarization)
###### tags: `leetcode`, `leetcode-easy`, `binary tree`