Dado un árbol binario y un entero K , la tarea es eliminar todos los Nodes que son múltiplos de K del árbol binario dado.
Ejemplos:
Input: 1 / \ 2 3 / \ / 4 5 8 / \ / 6 7 9 Output: Level Order Traversal of Given Binary Tree: 1 2 3 4 5 8 6 7 9 Level Order Traversal of Updated Binary Tree: 1 5 3 7 9
Acercarse:
- Convierta el árbol binario dado en una lista doblemente enlazada .
- Elimine todos los Nodes que sean múltiplos de K de la lista doblemente enlazada creada.
- Convierta la lista doblemente enlazada actualizada de nuevo en un árbol binario.
A continuación se muestra la implementación del enfoque anterior:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; struct node { int data; node *left, *right; }; node* newnode(int data) { node* temp = new node; temp->data = data; temp->left = temp->right = NULL; return temp; } // Level order traversal of the tree void printTree(node* root) { if (!root) return; queue<node*> q; q.push(root); while (!q.empty()) { int n = q.size(); while (n--) { node* temp = q.front(); q.pop(); cout << temp->data << " "; if (temp->left) q.push(temp->left); if (temp->right) q.push(temp->right); } cout << endl; } } // Function to convert binary tree // to doubly linked list void BT2DLL(node* root, node*& head) { // Base Case if (root == NULL) return; static node* left = NULL; // Traverse the left subtree BT2DLL(root->left, head); // Assign head to leftmost node if (left == NULL) head = root; else { root->left = left; left->right = root; } left = root; // Traverse the right subtree BT2DLL(root->right, head); } // Function to delete the current node // from the doubly linked list void deletenode(node*& head, node* del) { // Base case if (head == NULL || del == NULL) return; // If node to be deleted is head node if (head == del) head = del->right; // Change right only if node to be deleted // is NOT the last node if (del->right != NULL) del->right->left = del->left; // Change left only if node to be deleted // is NOT the first node if (del->left != NULL) del->left->right = del->right; // Finally, free the memory occupied by del free(del); } // Function to remove the multiples of k // from doubly linked list void removeKeys(node*& head, int k) { if (head == NULL) return; node* current = head; node* right; while (current != NULL) { // If multiple of k then remove this node if (current->data % k == 0) { // Save current's right node in the // pointer 'right' right = current->right; // Delete the node pointed to by // 'current' deletenode(head, current); // Update current current = right; } // Else simply move to the right node else current = current->right; } } // Function to convert doubly linked list // to binary tree node* DLLtoBT(node*& head, int n) { if (n <= 0) return NULL; node* left = DLLtoBT(head, n / 2); node* root = head; root->left = left; head = head->right; root->right = DLLtoBT(head, n - n / 2 - 1); return root; } // Function that removes the multiples // of k from the given binary tree node* removeMultiplesUtil(node* root, int k) { node* head = NULL; // Convert Binary Tree to DLL BT2DLL(root, head); // Remove multiples of k from DLL removeKeys(head, k); node* temp = head; int n = 0; // Get Size of the updated list while (temp) { ++n; temp = temp->right; } // Convert DLL to Binary Tree return DLLtoBT(head, n); } // Function to call the utility function // that removes the multiples of k // from the given binary tree void removeMultiples(node*& root, int k) { if (!root) return; root = removeMultiplesUtil(root, k); } // Driver code int main() { int k = 2; node* root = newnode(1); root->left = newnode(2); root->right = newnode(3); root->left->left = newnode(4); root->left->right = newnode(5); root->left->left->left = newnode(6); root->left->left->right = newnode(7); root->right->left = newnode(8); root->right->left->left = newnode(9); cout << "Level Order Traversal of Given Binary Tree:\n"; printTree(root); removeMultiples(root, k); cout << "\nLevel Order Traversal of Updated Binary Tree:\n"; printTree(root); return 0; }
Producción:
Level Order Traversal of Given Binary Tree: 1 2 3 4 5 8 6 7 9 Level Order Traversal of Updated Binary Tree: 1 5 3 7 9
Publicación traducida automáticamente
Artículo escrito por Aashish Chauhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA