-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114.cpp
36 lines (34 loc) · 908 Bytes
/
114.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
/**
* 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* solute(TreeNode* root)
{
if(root == NULL) return NULL;
if(root->left == NULL && root->right == NULL) return root;
TreeNode* left = solute(root->left);
TreeNode* right = solute(root->right);
if(left == NULL) return right;
if(right == NULL) {
root->right = root->left;
root->left = NULL;
return left;
}
left->right = root->right;
root->right = root->left;
root->left = NULL;
return right;
}
void flatten(TreeNode* root) {
solute(root);
}
};