克隆一个有随机指针的二叉树 Clone a Binary Tree with Random Pointers
2014-11-25 10:19
453 查看
一个二叉树的节点有如下结构
指针random随机地指向树中的任意一个节点,也可以指向NULL。写出函数,克隆这棵树。
思路:
1. Create new nodes in cloned tree and insert each new node in original tree between the left pointer edge of corresponding node in the original tree (See the below image).
i.e. if current node is A and it’s left child is B ( A — >> B ), then new cloned node with key A wil be created (say cA) and it will be put as A — >> cA — >> B (B can be a NULL or a non-NULL left child). Right child pointer will be set correctly i.e. if for
current node A, right child is C in original tree (A — >> C) then corresponding cloned nodes cA and cC will like cA —- >> cC
2. Set random pointer in cloned tree as per original tree
i.e. if node A’s random pointer points to node B, then in cloned tree, cA will point to cB (cA and cB are new node in cloned tree corresponding to node A and B in original tree)
3. Restore left pointers correctly in both original and cloned tree
下面给出step1和step3的两个函数,这两个函数的input是只有左右孩子的节点,但是可以很容易地把这两个函数扩展。
http://www.geeksforgeeks.org/clone-binary-tree-random-pointers/
struct node { int key; struct node *left,*right,*random; }
指针random随机地指向树中的任意一个节点,也可以指向NULL。写出函数,克隆这棵树。
思路:
1. Create new nodes in cloned tree and insert each new node in original tree between the left pointer edge of corresponding node in the original tree (See the below image).
i.e. if current node is A and it’s left child is B ( A — >> B ), then new cloned node with key A wil be created (say cA) and it will be put as A — >> cA — >> B (B can be a NULL or a non-NULL left child). Right child pointer will be set correctly i.e. if for
current node A, right child is C in original tree (A — >> C) then corresponding cloned nodes cA and cC will like cA —- >> cC
2. Set random pointer in cloned tree as per original tree
i.e. if node A’s random pointer points to node B, then in cloned tree, cA will point to cB (cA and cB are new node in cloned tree corresponding to node A and B in original tree)
3. Restore left pointers correctly in both original and cloned tree
下面给出step1和step3的两个函数,这两个函数的input是只有左右孩子的节点,但是可以很容易地把这两个函数扩展。
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; #include <string.h> #include <set> #include <stdio.h> #include <stdlib.h> #define INT_MIN -100 #define INT_MAX 100 struct Node{ int key; Node* left; Node* right; Node(int k, Node* l, Node* r): key(k), left(l), right(r){}; }; void copyLeftRight(Node* root) // step 1 { if(root) { copyLeftRight(root->left); copyLeftRight(root->right); Node* newRoot = new Node(root->key, NULL, NULL); newRoot->left = root->left; if(root->right) newRoot->right = root->right->left; root->left = newRoot; } } void restoreTree(Node* root) // step 3 { if(root) { Node*left = root->left; if(left) { root->left = left->left; if(root->left) left->left = root->left->left; } restoreTree(root->left); restoreTree(root->right); } } void printTree(Node* root) { if(root==NULL) return; cout<<root->key<<" "; printTree(root->left); printTree(root->right); } int main() { Node* n1 = new Node(1,NULL,NULL); Node* n3 = new Node(3,NULL,NULL); Node* n2 = new Node(2,n1,n3); Node* n5 = new Node(5,NULL,NULL); Node* n4 = new Node(4,n2,n5); Node* n0 = new Node(0, NULL, n4); /* Constructed binary tree is 0 \ 4 / \ 2 5 / \ 1 3 */ copyLeftRight(n0); Node* n0_copy = n0->left; restoreTree(n0); printTree(n0); cout<<endl; printTree(n0_copy); }完整的解法见:
http://www.geeksforgeeks.org/clone-binary-tree-random-pointers/
相关文章推荐
- 数据结构——用随机指针克隆一个二叉树
- LeetCode(Validate Binary Search Tree) 判断一个二叉树是否是二叉搜索树
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
- LeetCode | Copy List with Random Pointer(赋值带有随机指针的链表)
- (不会)[LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
- Copy Binary Tree with Random Pointer
- [LintCode] 复制带随机指针的链表 Copy List with Random Pointer
- LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解
- 怎样判断一个二叉树是binary search tree
- Copy List with Random Pointer复制带有随机指针的链表
- 【LeetCode-面试算法经典-Java实现】【143-Copy List with Random Pointer(有随机指针的链表复制)】
- LeetCode OJ 之 Copy List with Random Pointer(复制含有随机指针的链表)
- Copy List with Random Pointer(复制有随机指针的链表)
- 【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】
- 附有随机结点指针的链表的深度拷贝 Copy List with Random Pointer
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- LeetCode(Balanced Binary Tree ) 判断一个二叉树是否为平衡二叉树
- ※ Leetcode - Tree - 226. Invert Binary Tree(反转二叉树 使用二级指针交换两个指针的地址)
- Copy List with Random Pointer 复制有随机指针的链表@LeetCode
- Leetcode:Validate Binary Tree 判断一个二叉树是否为二叉搜索树