-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path114-flatten.h
More file actions
55 lines (46 loc) · 791 Bytes
/
114-flatten.h
File metadata and controls
55 lines (46 loc) · 791 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
48
49
50
51
52
53
54
#pragma once
#include <iostream>
#include <vector>
#include <list>
#include "TreeDefine.h"
using namespace std;
/*
将二叉树展开为列表
-展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
-展开后的单链表应该与二叉树 先序遍历 顺序相同。
*/
class CSolution
{
public:
CSolution();
~CSolution();
public:
void flatten(TreeNode* root);
private:
};
CSolution::CSolution()
{
}
CSolution::~CSolution()
{
}
//将二叉树展开为列表
void CSolution::flatten(TreeNode* root)
{
while (root)
{
if (root->left) {
//find right leaf node
TreeNode* pre = root->left;
while (pre) {
pre = pre->right;
}
//
pre->right = root->right;
root->right = root->left;
root->left = nullptr;
}
//
root = root->right;
}
}