您的位置:首页 > 其它

《Cracking the Coding Interview》——第17章:普通题——题目13

2014-04-29 00:20 513 查看
2014-04-29 00:15

题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成。

解法:Leetcode上也有,递归解法。

代码:

// 17.13 Flatten a binary search tree into a doubly linked list by inorder traversal order.
// Use postorder traversal to do the flattening job.
#include <cstdio>
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = 0): val(_val), left(nullptr), right(nullptr) {};
};

void flatten(TreeNode *&root, TreeNode *&left_most, TreeNode *&right_most)
{
if (root == nullptr) {
left_most = right_most = nullptr;
return;
}

TreeNode *ll, *lr, *rl, *rr;
if (root->left != nullptr) {
flatten(root->left, ll, lr);
root->left = lr;
lr->right = root;
} else {
ll = lr = root;
}

if (root->right != nullptr) {
flatten(root->right, rl, rr);
root->right = rl;
rl->left = root;
} else {
rl = rr = root;
}

left_most = ll;
right_most = rr;
}

void constructBinaryTree(TreeNode *&root)
{
int val;

if (scanf("%d", &val) != 1) {
root = nullptr;
} else if (val == 0) {
root = nullptr;
} else {
root = new TreeNode(val);
constructBinaryTree(root->left);
constructBinaryTree(root->right);
}
}

void deleteList(TreeNode *&head)
{
TreeNode *ptr;

while (head != nullptr) {
ptr = head;
head = head->right;
delete ptr;
}
}

int main()
{
TreeNode *root;
TreeNode *left_most, *right_most;
TreeNode *head;
TreeNode *ptr;

while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
}
flatten(root, left_most, right_most);
head = left_most;
for (ptr = head; ptr != nullptr; ptr = ptr->right) {
printf("%d ", ptr->val);
}
putchar('\n');
deleteList(head);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐