[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 ...
随机推荐
- xcrun: error: active developer path ("/XX") does not exist
MAC OS 10.9下执行命令 svn --version 报出如下错误: xcrun: error: active developer path ("/Users/username/Do ...
- GitHub 使用教程图文详解(转)
大纲: 一.前言 二.GitHub简介 三.注册GitHub账号 四.配置GitHub 五.使用GitHub 六.参与GitHub中其它开源项目 七.总结 注,GitHub官网:https://git ...
- bzoj专题训练
//http://blog.csdn.net/PoPoQQQ/article/category/2542243
- 【转】最近搞Hadoop集群迁移踩的坑杂记
http://ju.outofmemory.cn/entry/237491 Overview 最近一段时间都在搞集群迁移.最早公司的hadoop数据集群实在阿里云上的,机器不多,大概4台的样子,据说每 ...
- Android 插件化 动态升级
最新内容请见原文:Android 插件化 动态升级 不少朋友私信以及 Android开源交流几个 QQ 群 中都问到这个问题,这里简单介绍下 1.作用 大多数朋友开始接触这个问题是因为 App 爆棚了 ...
- 【JavaScript基础学习】关于正则表达式的完整内容
w3cJavaScript RegExp对象 这个如果第一次看的话应该会很莫名其妙,但可以看一遍留个印象. 正则表达式30分钟入门教程 这个教程非常完整,走一遍大概能够明白怎么回事了. 正则表达式在 ...
- AngularJS学习之模块
1.模块定义了一个应用程序:模块是应用程序中不同部分的容器:模块是应用控制器的容器:控制器通常属于一个模块 2.创建模块:你可以通过AngularJS的angular.module函数来创建模块: & ...
- POJ 3274 HASH
题目链接:http://poj.org/problem?id=3274 题意+思路: 点击这里 补充:因为有减法运算,所以可能会造成运算后结果为负数,所以需要把结果统一转换成正数[不然数组下标访问不到 ...
- jquery笔记(基础知识)
最近在学jquery,做点小笔记 语法: $(this).hide() - 隐藏当前元素 $("p").hide() - 隐藏所有 <p> 元素 $("p . ...
- keytool创建Keystore和Trustsotre文件
一.生成一个含有一个私钥的keystore文件 user@ae01:~$ keytool -genkey -keystore keystore -alias jetty-azkaban -keyalg ...