面试OR笔试12——二叉树转化为链表

来源:互联网 发布:cf游戏数据异常怎么办 编辑:程序博客网 时间:2024/06/11 03:24

题目及要求

1.1 题目描述

把一个二叉树转化为链表(单双向都可以)。

 

2 解答

2.1 代码

#include <iostream>using namespace std;// 树的结构struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};//********************************************************//// 带有哨兵的实现, dump后面(.right->)接着要求的链表TreeNode* treeToList1_dump(TreeNode *root, TreeNode &dump) {if (!root) {dump.right = nullptr;return &dump;}TreeNode *np, *retp(root);np = treeToList1_dump(root->right, dump);if (root->right = dump.right) retp = np;np = treeToList1_dump(root->left, dump);np->right = root;root->left = nullptr;// root->left = np;// 双向链表return retp;}TreeNode* treeToList1_dump(TreeNode *root) {TreeNode dump(0);treeToList1_dump(root, dump);return dump.right;}//********************************************************//// 单链表实现, 修改注释处也可以改为双链表void treeToList1(TreeNode *root, TreeNode **head, TreeNode **tail) {if (root->left) {treeToList1(root->left, head, tail);(*tail)->right = root;root->left = nullptr;//root->left = *tail;// 双链表}else {*head = root;}if (root->right) {TreeNode *np;treeToList1(root->right, &np, tail);root->right = np;//np->left = root;// 双链表}else {*tail = root;}}TreeNode* treeToList1(TreeNode *root) {if (!root) return root;TreeNode *head(nullptr), *tail(nullptr);treeToList1(root, &head, &tail);return head;}//********************************************************//// 循环双链表实现int connect(TreeNode *p1, TreeNode *p2) { // 连接两个环形链表  if (!(p1 && p2)) return -1;p2->left->right = p1->right;p1->right->left = p2->left;p1->right = p2;p2->left = p1;return 0;}TreeNode* treeToList2_m(TreeNode *root) { // 转化为环形链表, 并返回首元素指针  if (!root) return root;TreeNode *np(root), *lp(root->left), *rp(root->right);root->left = root->right = root;if (lp) connect((np = treeToList2_m(lp))->left, root);if (rp) connect(root, treeToList2_m(rp));return np;}TreeNode* treeToList2(TreeNode *root) { // 断开环形链表, 并返回首元素指针  if (!root) return root;root = treeToList2_m(root);root->left->right = nullptr;root->left = nullptr;return root;}int main() {TreeNode t[] = { 0,1,2,3,4,5,6 };t[3].left = t + 1; t[3].right = t + 5;t[1].left = t; t[1].right = t + 2;t[5].left = t + 4; t[5].right = t + 6;auto np = treeToList1_dump(t + 3);for (auto np1(np);np1;np1 = np1->right) cout << np1->val << ' ';cout << endl;t[3].left = t + 1; t[3].right = t + 5;t[1].left = t; t[1].right = t + 2;t[5].left = t + 4; t[5].right = t + 6;t[0].left = t[0].right = t[2].left = t[2].right = nullptr;t[4].left = t[4].right = t[6].left = t[6].right = nullptr;np = treeToList1(t + 3);for (auto np1(np);np1;np1 = np1->right) cout << np1->val << ' ';cout << endl;t[3].left = t + 1; t[3].right = t + 5;t[1].left = t; t[1].right = t + 2;t[5].left = t + 4; t[5].right = t + 6;t[0].left = t[0].right = t[2].left = t[2].right = nullptr;t[4].left = t[4].right = t[6].left = t[6].right = nullptr;np = treeToList2(t + 3);for (auto np1(np);np1;np1 = np1->right) cout << np1->val << ' ';cout << endl;return 0;}