# 226. Invert Binary Tree

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root!=NULL)
{
TreeNode *temp=root->left;
root->left=invertTree(root->right);
root->right=invertTree(temp);
}
return root;
}
};

//recursive
TreeNode* invertTree(TreeNode* root) {
if (root) {
invertTree(root->left);
invertTree(root->right);
std::swap(root->left, root->right);
}
return root;
}

//non-recursive
TreeNode* invertTree(TreeNode* root) {
std::stack<TreeNode*> stk;
stk.push(root);

while (!stk.empty()) {
TreeNode* p = stk.top();
stk.pop();
if (p) {
stk.push(p->left);
stk.push(p->right);
std::swap(p->left, p->right);
}
}
return root;
}

#### 【LeetCode】226. Invert Binary Tree 解题报告

2016-05-29 00:18:13

#### 226. Invert Binary Tree [easy] (Python)

2016-05-12 14:36:34

#### leetcode 226 Invert Binary Tree 翻转二叉树

2015-06-23 23:53:58

#### Leetcode 226: Invert Binary Tree(二叉树反转 递归、非递归实现)

2015-06-13 14:41:43

#### PAT_A 1102. Invert a Binary Tree (25)

2016-10-23 10:19:12

#### C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

2018-06-10 15:56:23

#### 1102 Invert a Binary Tree

2016-02-25 16:49:43

#### Invert Binary Tree 递归

2016-05-15 16:45:56

#### 226. Invert Binary Tree *

2016-11-29 17:52:01

#### 226. Invert Binary Tree

2018-06-10 10:21:52