forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-upside-down_1_AC.cpp
More file actions
44 lines (39 loc) · 947 Bytes
/
binary-tree-upside-down_1_AC.cpp
File metadata and controls
44 lines (39 loc) · 947 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 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* upsideDownBinaryTree(TreeNode* root) {
if (root == NULL) {
return NULL;
}
TreeNode *p1, *p2;
TreeNode *pl, *pr;
TreeNode *next_pl, *next_pr;
p1 = root;
p2 = p1->left;
pl = pr = NULL;
while (true) {
next_pl = p1->right;
next_pr = p1;
p1->left = pl;
p1->right = pr;
pl = next_pl;
pr = next_pr;
p1 = p2;
if (p1 != NULL) {
root = p1;
p2 = p2->left;
} else {
break;
}
}
return root;
}
};