最近的一次编码采访中提到了这个问题。
问:给定二叉树,编写程序将其转换为双向链表。双向链表中的节点按照之字形级别遍历形成的顺序排列
我的方法
我总是可以对树的zig-zag级别顺序进行遍历并将其存储在数组中 然后制作双链表。 但问题需要一个就地解决方案。 任何人都可以帮忙解释应该使用递归方法吗?
答案 0 :(得分:13)
这是递归方法。注意,这里root将指向所形成列表的某个inbetween元素。所以,只需从根向后遍历即可获得头部。
#define NODEPTR struct node*
NODEPTR convert_to_ll(NODEPTR root){
if(root->left == NULL && root->right == NULL)
return root;
NODEPTR temp = NULL;
if(root->left != NULL){
temp = convert_to_ll(root->left);
while(temp->right != NULL)
temp = temp->right;
temp->right = root;
root->left = temp;
}
if(root->right != NULL){
temp = convert_to_ll(root->right);
while(temp->left != NULL)
temp = temp->left;
temp->left = root;
root->right = temp;
}
return root;
}
答案 1 :(得分:4)
最简单的方法。在单个inorder遍历中,只有O(1)空间复杂度,我们可以实现这一点。 保留一个名为 lastPointer 的指针,并在访问每个节点后跟踪它。 使用左右
public void toll(T n) {
if (n != null) {
toll(n.left);
if(lastPointer==null){
lastPointer=n;
}else{
lastPointer.right=n;
n.left=lastPointer;
lastPointer=n;
}
toll(n.right);
}
}
答案 2 :(得分:3)
C ++代码:
Node<T> *BTtoDoublyLLZigZagOrder(Node<T> *root)
{
if (root == 0)
return 0;
if (root->mLeft == 0 && root->mRight == 0)
return root;
queue<Node<T> *> q;
q.push(root);
Node<T> *head = root;
Node<T> *prev = 0,*curr = 0;
while(!q.empty())
{
curr = q.front();
q.pop();
if (curr->mLeft)
q.push(curr->mLeft);
if (curr->mRight)
q.push(curr->mRight);
curr->mRight = q.front();
curr->mLeft = prev;
prev = curr;
}
return head;
}
答案 3 :(得分:2)
stanford库链接中提到的解决方案是BST到循环DLL的完美解决方案,下面的解决方案并不完全是BST到循环DLL的转换,但是循环DLL可以通过连接DLL的末端来实现。它不完全是zig zag有序树到dll转换。
注意:此解决方案不是从BST到循环DLL的完美转换,但它是一个易于理解的黑客
JAVA代码
public Node bstToDll(Node root ){
if(root!=null){
Node lefthead = bstToDll(root.left); // traverse down to left
Node righthead = bstToDll(root.right); // traverse down to right
Node temp = null;
/*
* lefthead represents head of link list created in left of node
* righthead represents head of link list created in right
* travel to end of left link list and add the current node in end
*/
if(lefthead != null) {
temp = lefthead;
while(temp.next != null){
temp = temp.next;
}
temp.next = root;
}else{
lefthead = root;
}
root.prev = temp;
/*
*set the next node of current root to right head of right list
*/
if(righthead != null){
root.next = righthead;
righthead.prev = root;
}else{
righthead = root;
}
return lefthead;// return left head as the head of the list added with current node
}
return null;
}
希望它有所帮助
答案 4 :(得分:1)
无全局变量的反向有序遍历-c#实现。调用时, null 将首先传递给 right 参数。最终返回值是双向链接列表的 head
public static Node ToDLL(Node node, Node right)
{
if (node == null)
return null;
var rnd = ToDLL(node.Right, right);
if (rnd != null)
{
node.Right = rnd;
rnd.Left = node;
}
else
{
node.Right = right;
if (right!= null)
right.Left= node;
}
return ToDLL(node.Left, node) ?? node;
}
答案 5 :(得分:0)
我们将使用头部和尾部的两个前哨节点并对树进行有序遍历。第一次我们必须将头部链接到最小节点,反之亦然,并且还将最小节点链接到尾部,反之亦然。在第一次之后,我们只需要重新链接当前节点和尾部,直到遍历完成。在遍历后,我们将删除前哨节点并正确地重新连接头部和尾部。
public static Node binarySearchTreeToDoublyLinkedList(Node root) {
// sentinel nodes
Node head = new Node();
Node tail = new Node();
// in-order traversal
binarySearchTreeToDoublyLinkedList(root, head, tail);
// re-move the sentinels and re-link;
head = head.right;
tail = tail.left;
if (head != null && tail != null) {
tail.right = head;
head.left = tail;
}
return head;
}
/** In-order traversal **/
private static void binarySearchTreeToDoublyLinkedList(Node currNode, Node head, Node tail) {
if (currNode == null) {
return;
}
// go left
//
binarySearchTreeToDoublyLinkedList(currNode.left, head, tail);
// save right node for right traversal as we will be changing current
// node's right to point to tail
//
Node right = currNode.right;
// first time
//
if (head.right == null) {
// fix head
//
head.right = currNode;
currNode.left = head;
// fix tail
//
tail.left = currNode;
currNode.right = tail;
} else {
// re-fix tail
//
Node prev = tail.left;
// fix current and tail
//
tail.left = currNode;
currNode.right = tail;
// fix current and previous
//
prev.right = currNode;
currNode.left = prev;
}
// go right
//
binarySearchTreeToDoublyLinkedList(right, head, tail);
}
答案 6 :(得分:0)
node* convertToDLL(node* root, node*& head, node*& tail)
{
//empty tree passed in, nothing to do
if(root == NULL)
return NULL;
//base case
if(root->prev == NULL && root->next == NULL)
return root;
node* temp = NULL;
if(root->prev != NULL)
{
temp = convertToDLL(root->prev, head, tail);
//new head of the final list, this will be the left most
//node of the tree.
if(head == NULL)
{
head=temp;
tail=root;
}
//create the DLL of the left sub tree, and update t
while(temp->next != NULL)
temp = temp->next;
temp->next = root;
root->prev= temp;
tail=root;
}
//create DLL for right sub tree
if(root->next != NULL)
{
temp = convertToDLL(root->next, head, tail);
while(temp->prev != NULL)
temp = temp->prev;
temp->prev = root;
root->next = temp;
//update the tail, this will be the node with the largest value in
//right sub tree
if(temp->next && temp->next->val > tail->val)
tail = temp->next;
else if(temp->val > tail->val)
tail = temp;
}
return root;
}
void createCircularDLL(node* root, node*& head, node*& tail)
{
convertToDLL(root,head,tail);
//link the head and the tail
head->prev=tail;
tail->next=head;
}
int main(void)
{
//create a binary tree first and pass in the root of the tree......
node* head = NULL;
node* tail = NULL;
createCircularDLL(root, head,tail);
return 1;
}
答案 7 :(得分:0)
struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;
Node * create_node(int value){
Node * temp = (Node *)malloc(sizeof(Node));
temp->value = value;
temp->right= NULL;
temp->left = NULL;
return temp;
}
Node * addNode(Node *node, int value){
if(node == NULL){
return create_node(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}
void treeToList(Node *node){
Queue *queue = NULL;
Node * last = NULL;
if(node == NULL)
return ;
enqueue(&queue, node);
while(!isEmpty(queue)){
/* Take the first element and put
both left and right child on queue */
node = front(queue);
if(node->left)
enqueue(&queue, node->left);
if(node->right)
enqueue(&queue, node->right);
if(last != NULL)
last->right = node;
node->left = last;
last = node;
dequeue(&queue);
}
}
/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree
root = addNode(root,30);
root = addNode(root,20);
root = addNode(root,15);
root = addNode(root,25);
root = addNode(root,40);
root = addNode(root,37);
root = addNode(root,45);
treeToList(root);
return 0;
}
可以在以下位置找到队列API的实现 http://www.algorithmsandme.com/2013/10/binary-search-tree-to-doubly-linked.html
答案 8 :(得分:0)
我们可以使用inorder遍历并跟踪以前访问过的节点。对于每个被访问节点,可以分配前一个节点右侧和当前节点。
void BST2DLL(node *root, node **prev, node **head)
{
// Base case
if (root == NULL) return;
// Recursively convert left subtree
BST2DLL(root->left, prev, head);
if (*prev == NULL) // first iteration
*head = root;
else
{
root->left = *prev;
(*prev)->right = root;
}
*prev = root; // save the prev pointer
// Finally convert right subtree
BST2DLL(root->right, prev, head);
}
答案 9 :(得分:0)
我意识到这已经很老了,但是我正在为面试准备解决这个问题,并且我意识到如果你考虑每个递归函数调用,更新并返回链表的当前头部,它就会简单得多。如果你有兴趣返回头部,那么使用逆序遍历也会更好。将头部传递到递归函数也不需要静态或全局变量。这是Python代码:
def convert(n, head=None):
if n.right:
head = convert(n.right, head)
if head:
head.left = n
n.right = head
if n.left:
head = convert(n.left, n)
else:
head = n
return head
希望这对某人有用。
答案 10 :(得分:0)
这是Java代码。复杂度为O(N)。我还为此问题添加了一些测试用例。
#include <iostream>
#include <fstream>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
#define PORT 8080
int main(int argc, char** argv) {
int cliSockDes, readStatus;
struct sockaddr_in serAddr;
socklen_t serAddrLen;
char msg[] = "Hello!!!\n";
char buff[1024] = {0};
//create a socket
if ((cliSockDes = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket creation error...\n");
exit(-1);
}
//server socket address
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(PORT);
serAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (sendto(cliSockDes, msg, strlen(msg), 0, (struct sockaddr*)&serAddr, sizeof(serAddr)) < 0) {
perror("sending error...\n");
close(cliSockDes);
exit(-1);
}
serAddrLen = sizeof(serAddr);
readStatus = recvfrom(cliSockDes, buff, 1024, 0, (struct sockaddr*)&serAddr, &serAddrLen);
if (readStatus < 0) {
perror("reading error...\n");
close(cliSockDes);
exit(-1);
}
cout.write(buff, readStatus);
cout << endl;
close(cliSockDes);
return 0;
}
答案 11 :(得分:0)
找到有序的前驱并将左右指向当前根的前驱将为您完成这项工作。运行以下代码的时间复杂度为 O(N)
,并将占用辅助空间 O(H)
,其中 H = Height of the Tree
隐式用于递归堆栈。以下代码是使用 Python 3
编写的。
def convertToDLL(root):
# Base check
if root is None:
return root
# Converting left sub-tree to root
if root.left:
# Convert the left subtree
left = convertToDLL(root.left)
while left.right:
left = left.right
left.right = root
root.left = left
# Converting right sub-tree to root
if root.right:
# Convert the right subtree
right = convertToDLL(root.right)
while right.left:
right = right.left
right.left = root
root.right = right
return root
def bToDLL(root):
if root is None:
return root
# Convert to DLL
root = convertToDLL(root)
while root.left:
root = root.left
return root
def display(head):
# Display
if head is None:
return
while head:
print(head.data, end=" ")
head = head.right