使用队列反转二叉搜索树中的路径的C++代码
例如,给定一个二叉搜索树,我们需要从特定键反转其路径。
寻找解决方案的方法
在这种方法中,我们将创建一个队列并推送所有节点,直到获得根节点。
p>
示例
#include <bits/stdc++.h> using namespace std; struct node { int key; struct node *left, *right; }; struct node* newNode(int item){ struct node* temp = new node; temp->key = item; temp->left = temp->right = NULL; return temp; } void inorder(struct node* root){ if (root != NULL) { inorder(root->left); cout << root->key << " "; inorder(root->right); } } void Reversing(struct node** node, int& key, queue<int>& q1){ /* If the tree is empty then return*/ if (node == NULL) return; if ((*node)->key == key){ // if we find the key q1.push((*node)->key); // we push it into our queue (*node)->key = q1.front(); // we change the first queue element with current q1.pop(); // we pop the first element } else if (key < (*node)->key){ // if key is less than current node's value q1.push((*node)->key); // we push the element in our queue Reversing(&(*node)->left, key, q1); //we go to the left subtree using a recursive call (*node)->key = q1.front(); //we reverse the elements q1.pop(); // we pop the first element } else if (key > (*node)->key){ // if key greater than node key then q1.push((*node)->key);// we push node key into queue Reversing(&(*node)->right, key, q1);// we go to right subtree using a recursive call (*node)->key = q1.front();// replace queue front to node key q1.pop(); // we pop the first element } return; } struct node* insert_node(struct node* node, // function to insert node nodes in our BST int key){ if (node == NULL) return newNode(key); // if tree is empty we return a new node if (key < node->key) // else we push that in our tree node->left = insert_node(node->left, key); else if (key > node->key) node->right = insert_node(node->right, key); return node; // returning the node } int main(){ struct node* root = NULL; queue<int> q1; int k = 80; /****************Creating the BST*************************/ root = insert_node(root, 50); insert_node(root, 30); insert_node(root, 20); insert_node(root, 40); insert_node(root, 70); insert_node(root, 60); insert_node(root, 80); cout << "Before Reversing :" << "\n"; inorder(root); cout << "\n"; Reversing(&root, k, q1); cout << "After Reversing :" << "\n"; // print inorder of reverse path tree inorder(root); return 0; }
输出
Before Reversing : 20 30 40 50 60 70 80 After Reversing : 20 30 40 80 60 70 50
上述代码的解释
在这种方法中,我们只需搜索给定的键。当我们遍历树时,我们将所有节点放入队列中,现在当我们找到具有键值的节点时,我们交换排在前面的所有路径节点的值,在这个过程中,我们的路径
结论
我们使用队列和递归解决了 BST 中反转路径的问题。我们还学习了该问题的 C++ 程序以及解决该问题的完整方法(普通)。我们可以用其他语言比如C、java、python等语言来编写同样的程序。我们希望本教程对您有所帮助。
以上是使用队列反转二叉搜索树中的路径的C++代码的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

队列技术在PHP与MySQL中的消息延迟和消息重试的应用摘要:随着Web应用程序的不断发展,对于高并发处理和系统可靠性方面的需求越来越高。队列技术作为一种解决方案,被广泛应用于PHP与MySQL中,以实现消息延迟和消息重试的功能。本文将介绍队列技术在PHP与MySQL中的应用,包括队列的基本原理、使用队列实现消息延迟的方法和使用队列实现消息重试的方法,并给出

JavaQueue队列的性能分析与优化策略摘要:队列(Queue)是在Java中常用的数据结构之一,广泛应用于各种场景中。本文将从性能分析和优化策略两个方面来探讨JavaQueue队列的性能问题,并给出具体的代码示例。引言队列是一种先进先出(FIFO)的数据结构,可用于实现生产者-消费者模式、线程池任务队列等场景。Java提供了多种队列的实现,例如Arr

如何反转和倒序PHP数组在PHP中,数组是一种常用的数据结构,能够存储和操作大量的数据。有时我们需要对数组进行反转或者倒序操作,以满足特定的需求。本文将介绍如何使用PHP对数组进行反转和倒序操作,并给出相应的代码示例。一、反转数组反转数组是指将数组中的元素按照原来的顺序进行相反的重新排列。PHP提供了多种方法来实现数组的反转,以下介绍两种常用的

Java中的队列是一种线性数据结构,具有多种功能。队列有两个端点,它遵循先进先出(FIFO)原则插入和删除其元素。在本教程中,我们将了解Java中队列的两个重要函数,它们是add()和Offer()。什么是队列?java中的队列是一个扩展了util和collection包的接口。元素在后端插入并从前端移除。java中的队列可以使用链表、DeQueue、优先级队列等类来实现。优先级队列是普通队列的扩展形式,每个元素都有一个优先级。队列的add()方法该方法用于向队列中插入元素。它将定义的元素(作为

队列在PHP与MySQL中的任务监控和任务调度的实现方案引言在现代的Web应用程序开发中,任务队列是非常重要的一项技术。通过队列,我们可以将一些需要在后台执行的任务排队,并通过任务调度来控制任务的执行时间和顺序。本文将介绍如何在PHP与MySQL中实现任务的监控和调度,并提供具体的代码示例。一、队列的工作原理队列是一种先进先出(FIFO)的数据结构,可以用来

PHP邮件队列系统的原理和实现方式是什么?随着互联网的发展,电子邮件已经成为人们日常生活和工作中必不可少的通信方式之一。然而,随着业务的增长和用户数量的增加,直接发送电子邮件可能会导致服务器性能下降、邮件发送失败等问题。为了解决这个问题,可以使用邮件队列系统来通过串行队列的方式发送和管理电子邮件。邮件队列系统的实现原理如下:邮件入队列当需要发送邮件时,不再直

例如,给定一个二叉搜索树,我们需要从特定键反转其路径。寻找解决方案的方法在这种方法中,我们将创建一个队列并推送所有节点,直到获得根节点。p>示例 #include<bits/stdc++.h>usingnamespacestd;structnode{ intkey; structnode*left,*right;};structnode*newNode(intitem){&nb

在这个问题中,我们将根据数组值对给定的字符串执行多次反向查询。解决问题的简单方法是根据给定的数组值存储每个字符串段。优化的方法使用的逻辑是,当两次反向相同的子字符串时
