Skip to content

Commit ea83bea

Browse files
Invert Binary Tree
1 parent 6554dc8 commit ea83bea

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

1.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
Invert Binary Tree
4+
------------------
5+
6+
Example:
7+
8+
Input:
9+
10+
4
11+
/ \
12+
2 7
13+
/ \ / \
14+
1 3 6 9
15+
Output:
16+
17+
4
18+
/ \
19+
7 2
20+
/ \ / \
21+
9 6 3 1
22+
*/
23+
24+
/**
25+
* Definition for a binary tree node.
26+
* struct TreeNode {
27+
* int val;
28+
* TreeNode *left;
29+
* TreeNode *right;
30+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
31+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
32+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
33+
* };
34+
*/
35+
36+
class Solution {
37+
public:
38+
TreeNode* invertTree(TreeNode* root) {
39+
if(root == NULL) return root;
40+
invertTree(root->left);
41+
invertTree(root->right);
42+
TreeNode* temp = root->left;
43+
root->left = root->right;
44+
root->right = temp;
45+
return root;
46+
}
47+
};

0 commit comments

Comments
 (0)