-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path114-Flatten-Binary-Tree-to-Linked-List.cpp
More file actions
37 lines (30 loc) · 1.18 KB
/
Copy path114-Flatten-Binary-Tree-to-Linked-List.cpp
File metadata and controls
37 lines (30 loc) · 1.18 KB
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
class Solution {
public:
void flatten(TreeNode* root) {
if(root) dfs(root);
}
TreeNode* dfs(TreeNode* node){
if(!node->left && !node->right) return node;
if(node->left){
if(!node->right){
node->right = node->left;
node->left = nullptr;
return dfs(node->right);
}
dfs(node->left)->right = node->right;
node->right = node->left;
node->left = nullptr;
}
return dfs(node->right);
}
};
/* 114. Flatten-Binary-Tree-to-Linked-List.cpp
//////////////////////////////////////////////////
Given the root of a binary tree, flatten the tree into a "linked list":
The "linked list" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The "linked list" should be in the same order as a pre-order traversal of the binary tree.
Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
//////////////////////////////////////////////////
*/