-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path114.cpp
More file actions
37 lines (30 loc) · 768 Bytes
/
114.cpp
File metadata and controls
37 lines (30 loc) · 768 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
// 114. Flatten Binary Tree to Linked List - https://leetcode.com/problems/flatten-binary-tree-to-linked-list
#include "bits/stdc++.h"
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
private:
TreeNode* prev = nullptr;
public:
void postorder(TreeNode* node) {
if (node == nullptr) { return; }
postorder(node->right);
postorder(node->left);
node->right = prev;
node->left = nullptr;
prev = node;
}
void flatten(TreeNode* root) {
postorder(root);
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}