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. iOS经典面试题(转)

    iOS经典面试题   前言 写这篇文章的目的是因为前两天同学想应聘iOS开发,从网上找了iOS面试题和答案让我帮忙看看.我扫了一眼,倒吸了一口冷气,仔细一看,气的发抖.整篇题目30多个没有一个答案是对 ...

  2. sql语句中----删除表数据的"三兄弟"

    说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...

  3. -webkit-text-size-adjust

    ios使用-webkit-text-size-adjust禁止调整字体大小 body{-webkit-text-size-adjust: 100%!important;} android使用以下代码, ...

  4. 【MongoDB】1.安装--以及简单使用

    第一次接触MongoDB    参考&粘贴:http://jingyan.baidu.com/article/ed15cb1b52b8661be2698162.html 一.安装 1.首先去官 ...

  5. Android Fragment学习笔记(二)----Fragment界面添加和管理

    Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...

  6. js 事件监听

    addEventListener() 方法 element.addEventListener(event, function, useCapture); 第一个参数是事件的类型 (如 "cl ...

  7. nodejs随记02

    Basic认证 检查报文头中Authorization字段,由认证方式和加密值构成: basic认证中,加密值为username:password,然后进行Base64编码构成; 获取username ...

  8. 浩瀚移动POS收银开单扫描解决方案PDA仓储系统,无线批发,移动批发,无线POS,无线销售APP-车销管理PDA

    适用范围 各种业态的批发商铺.批发市场.订货会.展销会.配送中心仓库…… 产品简介 随着移动技术与智能PDA设备的迅猛发展,中国已经跨步进入移动信息化社会.移动商务是移动信息社会的重要载体与形式,它开 ...

  9. 短信猫 TIdTCPServer TIdTCPClient

    短信猫 服务端: IdTCPServer1: TIdTCPServer; IdAntiFreeze1: TIdAntiFreeze; unit UnitSever; interface uses Wi ...

  10. JS(event事件)

    常用的event事件: 属性 此事件发生在何时... onabort 图像的加载被中断. onblur 元素失去焦点. onchange 域的内容被改变. onclick 当用户点击某个对象时调用的事 ...