forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTreeUpsideDown.cpp
69 lines (59 loc) · 1.83 KB
/
binaryTreeUpsideDown.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Source : https://oj.leetcode.com/problems/binary-tree-upside-down/
// Author : Hao Chen
// Date : 2014-11-17
/**********************************************************************************
* Given a binary tree where all the right nodes are either leaf nodes with
* a sibling (a left node that shares the same parent node) or empty,
*
* Flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes.
* Return the new root.
*
* For example:
* Given a binary tree {1,2,3,4,5},
* 1
* / \
* 2 3
* / \
* 4 5
* return the root of the binary tree [4,5,2,#,#,3,1].
* 4
* / \
* 5 2
* / \
* 3 1
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
**********************************************************************************/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
//using a dummy node to help to store the new tree
TreeNode dummy(0);
TreeNode *head = &dummy, *left=NULL, *right=NULL;
while ( root!=NULL ) {
//find the right & left
left = root->right;
right = root;
//move root the next
root = root->left;
//replace the right with current root
right->left = head->left;
right->right = head->right;
//move the dummy to the root
dummy.right = right;
dummy.left = left;
//reset the head to the root
head = &dummy;
}
return head->right;
}
};