---
# System prepended metadata

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

---

# \#144 Binary Tree Preorder Traversal
## *遍歷二元樹(root->root.left->root.right)*
## Log
 - build 20210512 by syhuang

## 非遞迴版
 - 非遞迴版的VLR
```javascript=
var preorderTraversal = function(root) {
    var r = [];
    var t = [];//暫存p.val
    var p = root;
    while(p || t.length>0){
        while(p){
            r.push(p.val);
            t.push(p);
            p = p.left;
        }
        p = t.pop();
        p = p.right;        
    }
    return r;
};
```
## 初見
 - 遞迴實作VLR
```javascript=
var r = [];
var preorderTraversal = function(root) {
    if(!root) return [];
    return r.concat(
        [root.val],
        preorderTraversal(root.left),
        preorderTraversal(root.right)
    );
};
```
## 備註
 - [#94 Binary Tree Inorder Traversal](articles/Leetcode/easy/94.md)
## 參考
###### tags: `leetcode`, `leetcode-easy`, `binary tree`