C++刷leetcode(114. 二叉树展开为链表)【递归】
本帖最后由 糖逗 于 2020-5-8 17:47 编辑题目描述:
给定一个二叉树,原地将它展开为链表。
例如,给定二叉树
1
/ \
2 5
/ \ \
3 4 6
将其展开为:
1
\
2
\
3
\
4
\
5
\
6
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
核心代码:
#include<iostream>
#include <malloc.h>
#include <vector>
#include <math.h>
#include <queue>
#include<algorithm>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL), right(NULL){
}
};
TreeNode* CreateTree(vector<int> input){
TreeNode* res = (TreeNode*)malloc(sizeof(TreeNode)*input.size());
for(int i = 0; i < input.size(); i++){
res.val = input;
res.left = NULL;
res.right = NULL;
}
for(int i= 0; i < input.size(); i++){
if(2*i+1 < input.size()){
res.left = &res;
}
if(2*i+2 < input.size()){
res.right = &res;
}
}
return res;
}
void middle(TreeNode* root, vector<vector<int> >& res, int left, int right, int depth){
if(root == NULL) return;
int insert = left + (right - left) / 2;
res = root->val;
middle(root->left, res, left, insert - 1, depth + 1);
middle(root->right, res, insert + 1, right, depth + 1);
}
int treeDepth(TreeNode* root){
if(root == NULL || root -> val == 0) return 0;
return max(treeDepth(root->left) + 1, treeDepth(root->right) + 1);
}
void PrintTree(TreeNode* root) {
int depth = treeDepth(root);
int width = pow(2, depth) - 1;
vector<vector<int> > res(depth, vector<int>(width, 0));
middle(root, res, 0, width - 1, 0);
for(int i = 0; i < res.size(); i++){
for(int j = 0; j < res.size();j++){
if(res == 0){
cout<< " ";
}
else{
cout << res;
}
}
cout << endl;
}
cout << "------------------" << endl;
}
void flatten(TreeNode* root){
if(root == NULL) return;
if(root -> left != NULL){
TreeNode* temp = root -> left;
while(temp -> right != NULL){
temp = temp -> right;
}
temp -> right = root -> right;
root -> right = root -> left;
root -> left = NULL;
}
flatten(root -> right);
}
void print_right_tree(TreeNode* root){
int i = 0;
while(root -> right != NULL){
if(root -> val != 0){
cout << root -> val << endl;
}
root = root -> right;
}
cout << root -> val << endl;
return;
}
int main(void){
cout << "please send the number for the tree" << endl;
vector<int> input;
int number;
while(cin >> number){
input.push_back(number);
}
TreeNode* root = CreateTree(input);
PrintTree(root);
flatten(root);
print_right_tree(root);
return 0;
}
注意事项:
1.参考链接:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/solution/di-gui-yu-fei-di-gui-si-lu-jian-dan-by-xiao-zhu-bu/ 本帖最后由 糖逗 于 2020-5-8 17:47 编辑
新增代码:
void flatten(TreeNode* root){
if(root == NULL) return;
if(root -> left != NULL){
TreeNode* temp = root -> left;
while(temp -> right != NULL){
temp = temp -> right;
}
temp -> right = root -> right;
root -> right = root -> left;
root -> left = NULL;
}
flatten(root -> right);
}
void print_right_tree(TreeNode* root){
int i = 0;
while(root -> right != NULL){
if(root -> val != 0){
cout << root -> val << endl;
}
root = root -> right;
}
cout << root -> val << endl;
return;
}
页:
[1]