BST
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8657   Accepted: 5277

Description

Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until the last level, and we can also find the maximum number by going down the right node. Now you are given some queries as "What are the minimum and maximum numbers in the subtree whose root node is X?" Please try to find answers for there queries.

Input

In the input, the first line contains an integer N, which represents the number of queries. In the next N lines, each contains a number representing a subtree with root number X (1 <= X <= 231 - 1).

Output

There are N lines in total, the i-th of which contains the answer for the i-th query.

Sample Input

2
8
10

Sample Output

1 15
9 11 算法分析:
此题目的意思是,按照原则建成的二叉树,每次我输入一个根节点的值,从当前根节点一直往左跑,跑到左边最底层,此时的该点,一定是以输入值为根节点的二叉树的
最小值,同理往右边跑,一直跑到底,则是最大值。此题目符合树状数组的结构特点。
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <iomanip>
#include <algorithm> using namespace std; int main()
{
int t;
int n;int m, dd; scanf("%d", &t);
while(t--)
{
scanf("%d", &m);
dd= m&(-m) ;
dd--;
//printf("%d\n", dd);
printf("%d %d\n", m-dd, m+dd );
}
return 0;
}

POJ 2309 BST(二叉搜索树)的更多相关文章

  1. 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. bst 二叉搜索树简单实现

    //数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...

  4. 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: BST树 ...

  5. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  6. 浅析BST二叉搜索树

    2020-3-25 update: 原洛谷日报#2中代码部分出现一些问题,详情见此帖.并略微修改本文一些描述,使得语言更加自然. 2020-4-9 update:修了一些代码的锅,并且将文章同步发表于 ...

  7. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  8. LeetCode #938. Range Sum of BST 二叉搜索树的范围和

    https://leetcode-cn.com/problems/range-sum-of-bst/ 二叉树中序遍历 二叉搜索树性质:一个节点大于所有其左子树的节点,小于其所有右子树的节点 /** * ...

  9. Leetcode938. Range Sum of BST二叉搜索树的范围和

    给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和. 二叉搜索树保证具有唯一的值. 示例 1: 输入:root = [10,5,15,3,7,null,18], L = 7 ...

随机推荐

  1. ubuntu系统克隆

    使用clonezilla,原文地址:http://www.linuxidc.com/Linux/2014-09/107117.htm 类似的一篇:http://storysky.blog.51cto. ...

  2. tomcat启动报异常(一)

    异常问题:Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web ...

  3. Java判断语句中判断条件的执行顺序

    if判断里面如果是多个条件的判断,会按照从前往后的顺序执行.基本所有语言都是一致的设计,这样做能提高程序的效率. 比如: if ( conditionA && conditionB ) ...

  4. Laravel composer自定义命令空间,方便引用第三方库

    第一步:自定义目录 在app文件夹下新建Library目录,做为第三方库的目录 第二步: 修改composer.json文件 autoload节点下的psr-4自定义命名空间 规则: key: 命名空 ...

  5. Unity -- 入门教程二

    为了接下来要做的小游戏,在这里我要小小的修改一下移动的代码. public class PlayerMove : MonoBehaviour { //定义移动的速度 public float Move ...

  6. PS 基础知识 .pat文件如何使用

    我下了pat文件单打不开,也下了ps 匿名 回答:5 人气:7 解决时间:2009-05-25 12:48 满意答案 将你下载的pat文件放到下面文件夹内,(这是默认安装地址),重新打开Photosh ...

  7. 高速创建和mysql表相应的java domain实体类

    今天创建了一个表有十几个字段,创建完之后必定要写一个与之相应的java domain实体类. 这不是反复的工作吗?为什么不先把这个表的全部的字段查出来,然后放到linux环境下,用sed工具在每一行的 ...

  8. UE-9260使用说明2

    生成镜像 1. U-boot 生成u-boot.bin文件 (1) Makefile ifeq ($(ARCH),arm) CROSS_COMPILE = endif 改动为 ifeq ($(ARCH ...

  9. java监听器演示样例

    监听器的原理是观察者模式.就像明星(事件源)聚拢了一群粉丝(观察者).当明星有啥举动的时候会通过粉丝们报道出去. 订阅信息.计算器button都是该原理的应用. 以下写了一个监听器的小样例: pack ...

  10. iOS学习之iOS沙盒(sandbox)机制和文件操作1

    iOS学习之iOS沙盒(sandbox)机制和文件操作 接上篇 iOS学习之iOS沙盒(sandbox)机制和文件操作(一) 我们看看如何获取应用程序沙盒目录.包括真机的沙盒的目录. 1.获取程序的H ...