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. 第四篇:SOUI资源文件组织

    什么是资源? 现代的软件只要有UI,基本上少不了资源. 资源是什么?资源就是在程序运行时提供固定的数据源的文件. 在MFC当道的时代,资源一般就是位图(Bitmap),图标(Icon),光标(Curs ...

  2. Mac系统修改Intellij Idea默认JDK版本

    Intellij IDEA 默认情况下,使用的jdk的版本是1.6,当第一次启动IDEA的时候,如果系统中未安装jdk,则系统会自动到苹果官网下载jdk安装文件.如果你的系统已经安装了jdk1.7或是 ...

  3. currentStyle

    用js的style属性可以获得html标签的样式,但是不能获取非行间样式. 解决方法: 在IE下可以用currentStyle; 在FF下用getComputedStyle; 然而,为了让其兼容,解决 ...

  4. How Kafka’s Storage Internals Work

    In this post I'm going to help you understand how Kafka stores its data. I've found understanding th ...

  5. 封装JavaScript的AJAX

    // 创建request对象 function createXMLHttpRequest() { try { return new XMLHttpRequest();//大多数浏览器 } catch ...

  6. kylin1.5新特性 new aggregation group

    终于啃完并理解了,我果然弱鸡.new aggregation group,是kylin 1.5的新特性:老版本中的agg是需要选中所有可能被使用的纬度字段,以供查询:但存在高纬度的查询需求,例如查询某 ...

  7. 2016.8.16 HTML5重要标签及其属性学习

    1.运用BootStrap的基本布局: 2.基本布局第二步: ] 3.BootStrap提供了一个class=”well“”类,可以给你种深度的感觉: 4.不是每一个类都是为了CSS,有些类创建出来只 ...

  8. BZOJ 3053 The Closest M Points

    [题目分析] 典型的KD-Tree例题,求k维空间中的最近点对,只需要在判断的过程中加上一个优先队列,就可以了. [代码] #include <cstdio> #include <c ...

  9. jQuery-品牌列表案例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. mongodb 连接和操作

    使用方法: 1.安装mongodb 2.配置环境变量 mac下: export PATH=/Users/sunbey/Documents/learn/mongodb/mongodb-osx-x86_6 ...