1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

    Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

    Output Specification:

    For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

    Sample Input:
    9
    1 6
    2 3
    -1 -1
    -1 4
    5 -1
    -1 -1
    7 -1
    -1 8
    -1 -1
    73 45 11 58 82 25 67 38 42
    
    Sample Output:
    58 25 82 11 38 67 45 73 42

解析:

1,二叉树的表示方法有三种,数组表示法,二叉树结构数组表示法,链表结构表示法,本题用的是结构数组表示法.

2,PAT的题目还是很普通的,掌握几种常用的数据结构设计方案,以及相应的算法,就可以搞定大部分题目.

3,先想几个简单的问题:

3.1 给定一个数子序列,构造二叉查找树是不唯一的;

3.2 不仅是不唯一,而且是可以构造出任意形状的二叉查找树;

3.3 同一个形状的二叉查找树,会不会有多个,不会,可以从根结点往下考虑,首先根结点必须相同,否则两棵树对应的左右两边个数不会相等,用数学归纳法知,所有元素一一对应相等;

3.4 中序遍历二叉查找树出来的结果就是元素递增排序,为什么?因为 {左子树}  < 根 < {右子树}, 那么再展开子树到叶子结点,再去掉{}后就是a1 < a2 < a3 < a4....这个样子;

3.5 反过来,一棵树中序遍历展开后是升序,是否这棵树是二叉查找树, 答案是, 从根结点往下考虑,用数学归纳法,所以二叉查找树还可定义,中序遍历升序,则为二叉查找树;

3.6 根据上面推出来的二叉查找树等价定义, 将二叉树的结点按中序遍历展开,把给定的元素按生序排列, 对应填入到结点中, 即可构造出一棵二叉查找树.

代码如下:

/*************************************************************************> File Name: 1099.c> Author: YueBo> Mail: yuebowhu@163.com> Created Time: Sun 21 May 2017 07:45:35 AM CST************************************************************************/#include <stdio.h>
#include <stdlib.h>int idx = 0;
int cmp(const void *a, const void *b)
{return *(int *)a - *(int *)b;
}typedef struct
{int data;int left;int right;
} node;void InorderBuildTree(node *treeArr, int *elements, int root)
{if (root == -1)return;InorderBuildTree(treeArr, elements, treeArr[root].left);treeArr[root].data = elements[idx];idx++;InorderBuildTree(treeArr, elements, treeArr[root].right);
}void printfLevelOrder(node *treeArr, int root)  
{  node qu[1024];  int front, rear;  front = rear = -1;  if (root == -1)  return;  rear++;  qu[rear].data = treeArr[root].data;  qu[rear].left = treeArr[root].left;  qu[rear].right = treeArr[root].right;  while (rear != front)  {  if (front != -1)  printf(" ");  printf("%d", qu[++front].data);  if (qu[front].left != -1)  {   rear++;  qu[rear].data = treeArr[qu[front].left].data;  qu[rear].left = treeArr[qu[front].left].left;  qu[rear].right = treeArr[qu[front].left].right;  }  if (qu[front].right != -1)  {   rear++;  qu[rear].data = treeArr[qu[front].right].data;  qu[rear].left = treeArr[qu[front].right].left;  qu[rear].right = treeArr[qu[front].right].right;  }  }  printf("\n");  
}  int main()
{int N;node treeArr[128];int i;int elements[128];scanf("%d", &N);for (i = 0; i < N; i++)scanf("%d%d", &treeArr[i].left, &treeArr[i].right);for (i = 0; i < N; i++)scanf("%d", elements+i);qsort(elements, N, sizeof(elements[0]), cmp);InorderBuildTree(treeArr, elements, 0);printfLevelOrder(treeArr, 0);return 0;
}

代码说明:上面的代码中在递归构造二叉查找树函数void InorderBuildTree(node *treeArr, int *elements, int root)使用了idx这个全局变量,以后再改进这段代码,争取使这个函数保持独立性.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421170.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql列属性auto(mysql笔记四)

常见的的是一个字段不为null存在默认值 没值得时候才去找默认值&#xff0c;可以插入一个null到 可以为null的行里 主键&#xff1a;可以唯一标识某条记录的字段或者字段的集合 主键设置 主键不可为null,声明时自动设置为not null 字段上设置 字段名 primary key定义完字段后 …

详解html结构之间的各个关系,层级关系(以列表为例)

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>层级关系查找元素</title></head><body><div id "div">hello<ul id ""><li>li1</li><li>li2</…

leetcode 242 有效的字母异位词 C++

和赎金信的思路一样 我想我本科时光是找不到女朋友了&#xff0c;哪怕是一个异性的好朋友也不会有了&#xff0c;男女比例4&#xff1a;1&#xff0c;哼 class Solution { public:bool isAnagram(string s, string t) {int table2[26]{0};for(char a:t){table2[a-a];}for(char…

1058. A+B in Hogwarts (20)

1058. AB in Hogwarts (20) 时间限制50 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueIf you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silve…

jquery post 同步异步总结

最近在测试,发现有些效果不对,最后发现是post的执行顺序问题,所以研究了下,写了以下总结 1.post被请求多次,解决方法: 连接加入随机数 rand""Math.random() $.post("/Control/webControl.ashx?rand "Math.random(), { Method: "LoginIn", Parem…

js对html节点的操作

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><style></style><script>function copy(){//克隆节点//1&#xff0c;得到要克隆的divvar div document.body.firstElementChild;//2,复制…

leetcode 141 环形链表 C++

两种方法一个空间O(n)&#xff0c;另一个O(1)&#xff0c;时间都是O(n)。 one class Solution { public:bool hasCycle(ListNode *head) {unordered_set<ListNode*>set;while(head){if(set.count(head))return true;set.insert(head);headhead->next;}return false;}…

1056. Mice and Rice (25)

1056. Mice and Rice (25) 时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueMice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map…

Java poi读取,写入Excel2003

Java poi读取,写入Excel2003 相关阅读&#xff1a;poi读写Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.htmljxl读写excel2003/2007:http://www.cnblogs.com/gavinYang/p/3576819.html package com.gavin.operational.excle;import java.io.FileInputStream; import …

leetcode 21 合并两个有序链表 C++

因为迭代比较好理解 所以我们在这里用递归 class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if(!l1) return l2;else if(!l2) return l1;else if(l1->val<l2->val){l1->nextmergeTwoLists(l1->next,l2);return l1;}else{l2->…

1061. 判断题(15)

1061. 判断题(15) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue判断题的评判很简单&#xff0c;本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分。 输入格式&#xff1a; 输入在第一行给出两个不超过100的正整数N和M&…

css和 js 改变html里面的定位。

css和 js 改变html里面的定位。&#xff08;三种方式&#xff09; <style type"text/css">#div1{border: 1px aquamarine solid;/*固定定位&#xff1a;此元素在整个网页的位置不变,固定某处不动*/position : fixed;left: 20px;top: 10px;}#div2{/*相对定位&am…

终端I/O之行控制函数

下列4个函数提供了终端设备的行控制能力。其中&#xff0c;filedes引用一个终端设备&#xff0c;否则出错返回&#xff0c;errno设置为ENOTTY。 #include <termios.h> int tcdrain(int filedes); int tcflow(int filedes, int action); int tcflush(int filedes, int que…

js 正则表达式实现文本验证

<style>.spa{/*js来改变span颜色*/color: red;}</style><script type"text/javascript">function checkIn(obj){//失去焦点&#xff08;鼠标离开文本框&#xff09;时验证//文本框的值var val obj.value;//拿到文本框的名字&#xff0c;用来做swi…

1060. 爱丁顿数(25)

1060. 爱丁顿数(25) 时间限制250 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力&#xff0c;还定义了一个“爱丁顿数”E&#xff0c;即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的…

leetcode 203 移除链表元素 C++

注意第一个元素的处理方法 今晚还有四个题要做&#xff0c;来不及解释了&#xff0c;上代码 class Solution { public:ListNode* removeElements(ListNode* head, int val) {struct ListNode*prenew ListNode(0,head);struct ListNode*temppre;if(!temp->next) return null…

unity3d由多个部分组成一个角色

摘自http://forum.unity3d.com/threads/16485-quot-stitch-multiple-body-parts-into-one-character-quot So I have many many models. Each has a skeleton, material, etc. I want to take some of these and combine them into one so I can apply animation commands to …

求质数的个数

/我买了一本书&#xff0c;意大利作家 保罗.乔尔达诺 得奖的一本小说&#xff0c;好奇这本书有多少页。但它每页页码都是质数&#xff0c;需求如下&#xff0c;1907页书&#xff0c;每个页数以质数命名&#xff0c;求有多少页。/ package funJoy;public class calBook {public…

leetcode 701 二叉搜索树的插入操作 C++ 递归和迭代

迭代 class Solution { public:TreeNode* insertIntoBST(TreeNode* root, int val) {if(!root) return new TreeNode(val);TreeNode*curroot;while(cur){if(val<cur->val){if(cur->left)curcur->left;else{cur->leftnew TreeNode(val);break;}}else if(val>…

1062. 最简分数(20)

1062. 最简分数(20) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue一个分数一般写成两个整数相除的形式&#xff1a;N/M&#xff0c;其中M不为0。最简分数是指分子和分母没有公约数的分数表示形式。 现给定两个不相等的正分数 N1/M1 和 N2/M…