---
# System prepended metadata

title: \#226 Invert Binary Tree
tags: [binary tree, leetcode, leetcode-easy]

---

# \#226 Invert Binary Tree
## *給定一binary tree root, 反轉left node和right node後回傳root*
## Log
 - build 20210502 by syhuang

## xxx
 - xxx
```javascript=
```
## 初見
 - 使用遞迴實作反轉
```javascript=
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    if(!root) return null;
    let leftNode = root.left;
    let rightNode = root.right;
    root.left = invertTree(rightNode);
    root.right = invertTree(leftNode);
    return root;
};
```
## 備註
 - 初見即畢業
 - 另有深度優先(DFS)和廣度優先(BFS)的實作, 見參考1
## 參考
 - [參考1](https://leetcode.com/problems/invert-binary-tree/discuss/399221/Clean-JavaScript-iterative-DFS-BFS-solutions)
###### tags: `leetcode`, `leetcode-easy`, `binary tree`