当前位置:网站首页>complete binary tree problem
complete binary tree problem
2022-08-03 22:45:00 【Chengqiuming】
An original problem description
BST - POJ 2309 - Virtual Judgehttps://vjudge.net/problem/POJ-2309
Two Inputs and Outputs
1 input
Line 1 contains an integer N representing the number of queries.In the next N lines, each line contains a number representing the subtree with root X.
2 output
N rows, where row i contains the answer to query i.
Three input and output samples
1 Input example
2
8
10
2 Sample output
1 15
9 11
Four Analysis
The rules of this problem can be followed. If n is an odd number, it must be a leaf node, and the maximum and minimum numbers are itself.Otherwise, find the level where n is located (the reciprocal level, the bottom layer is 0), and its level is the position i of the first 1 from the low bit in the binary representation of n (the last non-zero bit), such as the two of 6The hierarchy is 110, the position of the first 1 from the low position is 1, so 6 is in the first layer; the binary of 12 is 1100, and the position of the first 1 from the low position is 2, so 12 is in the second layer, as followsas shown in the figure.

i is the number of layers. It can be obtained that the left and right subtrees of n each have k = 2^i-1 nodes, then the minimum number is n-k, and the maximum number is n+k, then how to find 2^i??
Actually, if you want to get the last non-zero bit, you only need to invert the original number and then add 1. At this time, except for the last non-zero bit, the other bits are opposite to the original number, and the bitwise AND operation is directly performed with the original number.You can get the last non-zero bit.
Five algorithm design
1 Solve for logbit(n)=n&(-n).
2 Let see = lowbit(n)-1, output the minimum number n-k, the maximum number n+k.
Six codes
package poj2309;import java.util.Scanner;public class Poj2309 {static int lowbit(int n) {return n & (-n);}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int T, n, k;T = scanner.nextInt();while (T--> 0) {n = scanner.nextInt();k = lowbit(n) - 1;System.out.println((n - k) + " " + (n + k));}}}Seven Tests
Green is input, white is output

边栏推荐
- [b01lers2020]Life on Mars
- 如何创建一个Web项目
- Kotlin - 扩展函数和运算符重载
- Republish the lab report
- utlis thread pool
- Summary bug 】 【 Elipse garbled solution project code in Chinese!
- 483. Smallest Good Base
- node连接mysql数据库报错:Client does not support authentication protocol requested by server
- Golang Chapter 2: Program Structure
- 113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
猜你喜欢

113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself

What is Adobe?

BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记

易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元

Go开发工具GoLand V2022.2 来了——Go 工作区重大升级

封装、包、访问权限修饰符、static变量

Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
![[b01lers2020]Life on Mars](/img/d0/d5c9b7224542c8843ce29adc7ef713.png)
[b01lers2020]Life on Mars

网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)

Bytebase database schema change management tool
随机推荐
node连接mysql数据库报错:Client does not support authentication protocol requested by server
for循环练习题
[N1CTF 2018]eating_cms
亿流量大考(2):开发一套高容错分布式系统
HCIP BGP实验报告
Embedded systems: overview
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
golang写的存储引擎,基于b+树,mmap
剑指offer第22题-链表中倒数第K个节点
目标检测技术研究现状及发展趋势
投资性大于游戏性 NFT游戏到底是不是门好生意
Bytebase database schema change management tool
.NET6之MiniAPI(十四):跨域CORS(上)
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
Golang第二章:程序结构
老板:公司系统太多,能不能实现账号互通?
AOSP CameraLatencyHistogram的原理与使用
Causes of Mysql Disk Holes and Several Ways to Rebuild Tables
Embedded Systems: GPIO
for loop exercises