PAT 1099 Build A Binary Search Tree

18 篇文章 0 订阅

 建立二叉搜索树,id指的是在中序遍历过程中,对应节点的索引

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
/**
 * 建立二叉搜索树
 * @return
 */
 struct TreeNode{
     int id;
     int val;
     TreeNode* left;
     TreeNode* right;
 };

 using namespace std;
 int id;
 void preOrder(TreeNode* node){
     if(node== nullptr){
         return;
     }
     preOrder(node->left);
     node->id=id;
     id++;
     preOrder(node->right);

 }
 void levelOrder(TreeNode* root,vector<int>& vector1){
     queue<TreeNode*> queue1;
     queue1.push(root);
     while (!queue1.empty()){
         auto node=queue1.front();
         queue1.pop();
         vector1.push_back(node->val);
         if(node->left!=nullptr){
             queue1.push(node->left);
         }
         if(node->right!=nullptr){
             queue1.push(node->right);
         }
     }
 }
int main() {
    int N;
    cin>>N;
    TreeNode* nodes[N];
    for (int i = 0; i < N; ++i) {
        nodes[i]=new TreeNode();
    }
    for (int j = 0; j < N; ++j) {
        int s1,s2;
        cin>>s1>>s2;
        if(s1!=-1){
            nodes[j]->left=nodes[s1];
        }
        if(s2!=-1){
            nodes[j]->right=nodes[s2];
        }
    }
    int nums[N];
    for (int i= 0; i < N; ++i) {
        cin>>nums[i];
    }
    sort(nums,nums+N);
    preOrder(nodes[0]);
    for (int i = 0; i < N; ++i) {
        nodes[i]->val=nums[nodes[i]->id];
    }
    vector<int> vector1;
    levelOrder(nodes[0],vector1);
    cout<<vector1[0];
    for (int i = 1; i < N; ++i) {
        cout<<" "<<vector1[i];

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过先进的信息技术,为师生提供一个全面智能的感知环境和综合信息服务平台。该方案正处在从信息化第二阶段向第三阶段过渡的关键时期,致力于实现校园服务和管理的全面智能化。 方案的核心目标是构建一个集成的校园地理信息服务平台,通过该平台实现资产管理、房产信息管理、基础设施管理、绿化管理和能源监测管理等功能。同时,该平台将提供校园漫游、信息服务、指引服务、活动通知、用房服务和客流统计等多样化服务,以促进校园的和谐、绿色、平安和便捷。 在技术层面,智慧校园建设方案强调系统集成能力、数据统一分析能力、系统资源共享能力以及大数据集成处理能力。通过这些能力,可以构建统一的校园地理信息平台,提供综合的应用支撑和管理能力,实现系统平滑演进。 应用方向上,智慧校园建设方案围绕和谐校园、绿色校园、平安校园和掌上校园四个维度展开。和谐校园侧重于提供校园漫游、信息服务、指引服务等,增强师生的校园体验。绿色校园则关注资产管理和能源监测,推动校园的可持续发展。平安校园通过视频监控、数字巡更等手段,确保校园安全。掌上校园则利用移动设备,实现校园服务的随时随地访问。 最终,智慧校园建设方案将通过三维虚拟校史馆、720度成像技术等创新应用,提供身临其境的校园漫游体验,同时通过可视化管理和数据分析,优化校园资源配置和运营效率,实现校园管理的智能化和现代化。
【Solution】 To convert a binary search tree into a sorted circular doubly linked list, we can use the following steps: 1. Inorder traversal of the binary search tree to get the elements in sorted order. 2. Create a doubly linked list and add the elements from the inorder traversal to it. 3. Make the list circular by connecting the head and tail nodes. 4. Return the head node of the circular doubly linked list. Here's the Python code for the solution: ``` class Node: def __init__(self, val): self.val = val self.prev = None self.next = None def tree_to_doubly_list(root): if not root: return None stack = [] cur = root head = None prev = None while cur or stack: while cur: stack.append(cur) cur = cur.left cur = stack.pop() if not head: head = cur if prev: prev.right = cur cur.left = prev prev = cur cur = cur.right head.left = prev prev.right = head return head ``` To verify the accuracy of the code, we can use the following test cases: ``` # Test case 1 # Input: [4,2,5,1,3] # Output: # Binary search tree: # 4 # / \ # 2 5 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> 5 # Doubly linked list in reverse order: 5 <-> 4 <-> 3 <-> 2 <-> 1 root = Node(4) root.left = Node(2) root.right = Node(5) root.left.left = Node(1) root.left.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) # Test case 2 # Input: [2,1,3] # Output: # Binary search tree: # 2 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 # Doubly linked list in reverse order: 3 <-> 2 <-> 1 root = Node(2) root.left = Node(1) root.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) ``` The output of the test cases should match the expected output as commented in the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值