当前位置:网站首页>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
边栏推荐
猜你喜欢
pikachu Over permission 越权
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
override学习(父类和子类)
node连接mysql数据库报错:Client does not support authentication protocol requested by server
Boss: There are too many systems in the company, can you realize account interoperability?
2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用
override learning (parent and child)
Adobe是什么?
代码随想录笔记_动态规划_416分割等和子集
《数字经济全景白皮书》金融数字用户篇 重磅发布!
随机推荐
互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产
嵌入式系统:概述
How many way of calling a function?
工作小计 QT打包
重发布实验报告
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
UVa 1025 - A Spy in the Metro(白书)
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
如何基于WPF写一款数据库文档管理工具(二)
Codeup刷题笔记-简单模拟
Why do we need callbacks
网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
Pytest学习-setup/teardown
LabVIEW code generation error 61056
决策树、GBDT、XGBOOST树的可视化
Flink--Join以及Flink函数
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
L2-041 插松枝
466. Count The Repetitions
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication