当前位置:网站首页>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.
The value ofi 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
边栏推荐
- Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
- 互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产
- BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
- 2022/8/3 考试总结
- 使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
- October 2019 Twice SQL Injection
- Go开发工具GoLand V2022.2 来了——Go 工作区重大升级
- utlis 线程池
- 亿流量大考(2):开发一套高容错分布式系统
- Storage engine written by golang, based on b+ tree, mmap
猜你喜欢
嵌入式系统:时钟
Shell编程的条件语句
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
亿流量大考(2):开发一套高容错分布式系统
On the Qixi Festival of 2022, I will offer 7 exquisite confession codes, and at the same time teach you to quickly change the source code for your own use
老板:公司系统太多,能不能实现账号互通?
October 2019 Twice SQL Injection
藏宝计划TreasureProject(TPC)系统模式开发技术原理
What is the difference between the generator version and the viewer version?
随机推荐
Republish the lab report
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
golang写的存储引擎,基于b+树,mmap
FinClip最易用的智能电视小程序
LabVIEW code generation error 61056
藏宝计划TreasureProject(TPC)系统模式开发技术原理
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
嵌入式系统:GPIO
[b01lers2020]Life on Mars
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
PowerMockup 4.3.4::::Crack
Why do we need callbacks
为什么我们需要回调
Nine ways to teach you to read the file path in the resources directory
趣链的产品构架
The sword refers to the offer question 22 - the Kth node from the bottom in the linked list
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
RPA power business automation super order!
log4j-slf4j-impl cannot be present with log4j-to-slf4j
[b01lers2020]Life on Mars