-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path226.cpp
More file actions
28 lines (28 loc) · 711 Bytes
/
226.cpp
File metadata and controls
28 lines (28 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 226
// Title: Invert Binary Tree
// Link: https://leetcode.com/problems/invert-binary-tree
// Idea: Recursively invert the children.
// Difficulty: easy
// Tags: binary-tree, recursion
/**
* 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 == nullptr) return root;
TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
invertTree(root->left);
invertTree(root->right);
return root;
}
};