-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeUpsideDown.cc
More file actions
47 lines (41 loc) · 842 Bytes
/
Copy pathBinaryTreeUpsideDown.cc
File metadata and controls
47 lines (41 loc) · 842 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
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sstream>
#include <iostream>
#include <stack>
#include <vector>
#include <iterator>
#include <numeric>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
TreeNode *cur = root, *p = nullptr, *pr = nullptr;
while (cur) {
// Rotate
auto left = cur->left; // store
cur->left = pr;
pr = cur->right; // move on
cur->right = p;
// Move on left-wards
p = cur;
cur = left;
}
return p;
}
};
int main(int argc, char const *argv[]) {
/* code */
return 0;
}