[CareerCup] 17.13 BiNode 双向节点
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] 17.13 BiNode 双向节点的更多相关文章
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- [CareerCup] 17.14 Unconcatenate Words 断词
17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...
- [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 ...
- [CareerCup] 17.11 Rand7 and Rand5 随机生成数字
17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...
- [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 ...
- [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. 这道题让我们找书中单词出现 ...
- [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 ...
- [CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字
17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand ...
- [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 ...
随机推荐
- 10gRAC运行srvctl报错error while loading shared libraries:
数据库10g才会有这个错,因为11g的grid和oracle是分开的. [oracle@news01 orcl]$ srvctl /u01/app/oracle/db_1/jdk/jre/bin/ja ...
- 【项目经验】——JSON.parse() && JSON.stringify()
我们在做项目的时候,都知道序列化和反序列化,师哥说:"有正就有反,有来就有回!"的确,就是这样.然后我们在这里分享一下JSON.stringify() 和JSON.parse() ...
- Popupwindow 的简单实用,(显示在控件下方)
第一步: private PopupWindow mPopupWindow; 第二步:写一个popupwindow的布局文件XML <?xml version="1.0" e ...
- SQL.WITH AS.公用表表达式(CTE)(转)
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...
- 分享一个漂亮WPF界面框架创作过程及其源码(转)
本文会作为一个系列,分为以下部分来介绍: (1)见识一下这个界面框架: (2)界面框架如何进行开发: (3)辅助开发支持:Demo.模板.VsPackage制作. 框架源码如下所示. 本文介绍第(1) ...
- (转)qsort和sort
1.qsort函数: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)) ...
- Xamarin基础命名空间Microsoft.SqlServer.Server
Xamarin基础命名空间Microsoft.SqlServer.Server 该命名空间包含大量的类.接口和枚举,用于操作微软SQL Server数据库.该空间支持Xamarin.iOS和Xam ...
- 20145223《Java程序程序设计》第9周学习总结
20145223<Java程序设计>第9周学习总结 教材学习内容总结 第十六章:整合数据库 JDBC入门 1.JDBC简介: 2.JDBC主要分成两个部分,JDBC应用程序开发者接口和JD ...
- jquerymobile 基础教程
http://www.w3cplus.com/blog/tags/331.html?page=1
- Cache的使用
公共方法Add 将指定项添加到 Cache 对象,该对象具有依赖项.过期和优先级策略以及一个委托(可用于在从 Cache 移除插入项时通知应用程序). Equals(从 Object 继承) 已重载. ...