-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy paths1.cpp
More file actions
22 lines (22 loc) · 703 Bytes
/
Copy paths1.cpp
File metadata and controls
22 lines (22 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// OJ: https://leetcode.com/problems/binary-tree-upside-down/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
if (!root || !root->left) return root;
TreeNode *left = root->left, *right = root->right, *newRoot = NULL;
while (left) {
TreeNode *nextLeft = left->left, *nextRight = left->right;
if (!newRoot) root->left = root->right = NULL;
newRoot = left;
newRoot->left = right;
newRoot->right = root;
root = left;
left = nextLeft;
right = nextRight;
}
return newRoot;
}
};