17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other nodes. The data structure BiNode could be used to represent both a binary tree (where nodel is the left node and node2 is the right node) or a doubly linked list (where nodel is the previous node and node2 is the next node). Implement a
method to convert a binary search tree (implemented with BiNode) into a doubly linked list. The values should be kept in order and the operation should be performed in place (that is, on the original data structure).

这道题定义了一个双向节点BiNode的类,其既可以表示二叉树的结构,也可以表示为双向链表的结构,让我们把一个二叉树的结构转化为双向链表的结构。我们有很多种方法可以实现,首先来看一种建立一种新数据结构NodePair类,保存了链表的首节点和尾节点,用递归的方法来实现压平二叉树,参见代码如下:

解法一:

class BiNode {
public:
BiNode *node1;
BiNode *node2;
int data;
BiNode(int d): data(d), node1(NULL), node2(NULL) {}
}; class NodePair {
public:
BiNode *head;
BiNode *tail;
NodePair(BiNode *h, BiNode *t): head(h), tail(t) {}
}; void concat(BiNode *x, BiNode *y) {
x->node2 = y;
y->node1 = x;
} NodePair* convert(BiNode *root) {
if (!root) return NULL;
NodePair *part1 = convert(root->node1);
NodePair *part2 = convert(root->node2);
if (part1) concat(part1->tail, root);
if (part2) concat(root, part2->head);
return new NodePair(part1 ? part1->head : root, part2 ? part2->tail : root);
}

我们也可以不使用别的数据结构,那么我们如何返回链表的首结点和尾结点呢,我们可以返回首结点,然后通过遍历链表来找到尾结点,参见代码如下:

解法二:

int cnt = ;
class BiNode {
public:
BiNode *node1;
BiNode *node2;
int data;
BiNode(int d): data(d), node1(NULL), node2(NULL) {}
}; void concat(BiNode *x, BiNode *y) {
x->node2 = y;
y->node1 = x;
} BiNode* get_tail(BiNode *node) {
if (!node) return NULL;
while (node->node2) {
++cnt;
node = node->node2;
}
return node;
} BiNode* convert(BiNode *root) {
if (!root) return NULL;
BiNode *part1 = convert(root->node1);
BiNode *part2 = convert(root->node2);
if (part1) concat(get_tail(part1), root);
if (part2) concat(root, part2);
return part1 ? part1 : root;
}

还有一种方法是创建一个循环链表,当我们建立了循环链表,那么我们返回首结点时,尾结点可以通过head->node1直接找到,参见代码如下:

解法三:

class BiNode {
public:
BiNode *node1;
BiNode *node2;
int data;
BiNode(int d): data(d), node1(NULL), node2(NULL) {}
}; void concat(BiNode *x, BiNode *y) {
x->node2 = y;
y->node1 = x;
} BiNode* convert_to_circular(BiNode *root) {
if (!root) return NULL;
BiNode *part1 = convert_to_circular(root->node1);
BiNode *part3 = convert_to_circular(root->node2);
if (!part1 && !part3) {
root->node1 = root;
root->node2 = root;
return root;
}
BiNode *tail3 = part3 ? part3->node1 : NULL;
// Join left to root
if (!part1) concat(part3->node1, root);
else concat(part1->node1, root);
// Join right to root
if (!part3) concat(root, part1);
else concat(root, part3);
// Join right to left
if (part1 && part3) concat(tail3, part1);
return part1 ? part1 : root;
} BiNode* convert(BiNode *root) {
BiNode *head = convert_to_circular(root);
head->node1->node2 = NULL;
head->node1 = NULL;
return head;
}

CareerCup All in One 题目汇总

[CareerCup] 17.13 BiNode 双向节点的更多相关文章

  1. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  2. [CareerCup] 17.14 Unconcatenate Words 断词

    17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...

  3. [CareerCup] 17.12 Sum to Specific Value 和为特定数

    17.12 Design an algorithm to find all pairs of integers within an array which sum to a specified val ...

  4. [CareerCup] 17.11 Rand7 and Rand5 随机生成数字

    17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...

  5. [CareerCup] 17.10 Encode XML 编码XML

    17.10 Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a ...

  6. [CareerCup] 17.9 Word Frequency in a Book 书中单词频率

    17.9 Design a method to find the frequency of occurrences of any given word in a book. 这道题让我们找书中单词出现 ...

  7. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  8. [CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字

    17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand ...

  9. [CareerCup] 17.6 Sort Array 排列数组

    17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elem ...

随机推荐

  1. 如何在Salesforce中进行代码开发

    两种方式: 1):用Salesforce自带的在线开发模式 Setup --> App Setup --> Develop --> than you can select 'Page ...

  2. Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)

    题目链接:http://codeforces.com/problemset/problem/118/E 思路:首先要判断图是否是边双连通,这个Tarjan算法可以判断,若low[v] > dfn ...

  3. 【HTML】HTML特殊符号【转http://www.cnblogs.com/web-d/archive/2010/04/16/1713298.html】

    HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全.                   ...

  4. Android 在地图上画矩形

    point1=map.toMapPoint(400,426); point2=map.toMapPoint(600,640); initextext = new Envelope(point1.get ...

  5. nginx日志中文变成类似\xE9\xA6\x96\xE9\xA1\xB5-\xE6\x8E\xA8\xE8\x8D\x90的东西,治本方案

    这里:https://groups.google.com/forum/#!topic/openresty/NcRSb5gTmVU 主要是: 这与 ngx_lua 无关,是较新的 nginx 核心引入的 ...

  6. hdu 2546 饭卡 删除一个数的01背包

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  7. 领域模型(domain model)&贫血模型(anaemic domain model)&充血模型(rich domain model)

    领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...

  8. 系统表达式(z变换、DTFT、差分方程)之间的关系

  9. Ue4全景图制作设想

    官方有个Scene Capture Cube与Cube Rander Target. 之后再想办法生成文件就好了吧

  10. UVa12092 Paint the Roads(最小费用最大流)

    题目大概说一个n个点m条带权有向边的图,要给边染色,染色的边形成若干个回路且每个点都恰好属于其中k个回路.问最少要染多少边权和的路. 一个回路里面各个点的入度=出度=1,那么可以猜想知道各个点如果都恰 ...