Skip to content

Commit

Permalink
[CPP] 226. Invert Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
UnresolvedCold committed Apr 3, 2022
1 parent 188d21c commit cd9f44d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cpp/226-Invert-Binary-Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;
auto ptr = root;
auto temp = ptr->left;
ptr->left = ptr->right;
ptr->right = temp;

invertTree(ptr->left);
invertTree(ptr->right);

return root;
}
};

0 comments on commit cd9f44d

Please sign in to comment.