Full Binary Tree(二叉树找规律)

Description

In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a node labelled v its left child will be labelled 2 * v and its right child will be labelled 2 * v + 1. The root is labelled as 1.
You are given n queries of the form i, j. For each query, you have to print the length of the shortest path between node labelled i and node labelled j.
 

Input

First line contains n(1 ≤ n ≤ 10^5), the number of queries. Each query consists of two space separated integers i and j(1 ≤ i, j ≤ 10^9) in one line.

 

Output

For each query, print the required answer in one line.
 

Sample Input

5
1 2
2 3
4 3
1024 2048
3214567 9998877

Sample Output

1
2
3
1
44

Hint

题目意思:这是一个二叉树,如上图所示,给出二叉树上的而已两个点求出一点到另一点最短的距离或者说是步数。

解题思路:我们知道二叉树相当于一个自上而下的三角形,任意两个点在上方一定有一个点相交,称其为节点,我们只要找到那个节点就可以了,两点到节点的距离就是最短距离。

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int t;
 6     long long a,b,m,n,count;
 7     scanf("%d",&t);
 8     while(t--)
 9     {
10         scanf("%lld%lld",&a,&b);
11         count=0;
12         while(1)
13         {
14             if(a>b)///两个数较大的那一个减半上移,不断逼近节点
15             {
16                 a=a/2;
17                 count++;
18             }
19             if(b>a)
20             {
21                 b=b/2;
22                 count++;
23             }
24             if(b==a)
25                 break;
26         }
27         printf("%lld\n",count);
28 
29     }
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/wkfvawl/p/8670142.html

下面是用C语言实现的代码,判断一棵二叉树是否为完全二叉树。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; typedef struct Queue { TreeNode **data; int front; int rear; int size; } Queue; Queue *createQueue(int size) { Queue *q = (Queue *)malloc(sizeof(Queue)); q->data = (TreeNode **)malloc(sizeof(TreeNode *) * size); q->front = q->rear = 0; q->size = size; return q; } bool isEmpty(Queue *q) { return q->front == q->rear; } bool isFull(Queue *q) { return (q->rear + 1) % q->size == q->front; } void enqueue(Queue *q, TreeNode *node) { if (isFull(q)) { return; } q->data[q->rear] = node; q->rear = (q->rear + 1) % q->size; } TreeNode *dequeue(Queue *q) { if (isEmpty(q)) { return NULL; } TreeNode *node = q->data[q->front]; q->front = (q->front + 1) % q->size; return node; } bool isCompleteTree(TreeNode* root) { if (root == NULL) { return true; } Queue *q = createQueue(1000); bool flag = false; enqueue(q, root); while (!isEmpty(q)) { TreeNode *node = dequeue(q); if (node->left) { if (flag) { return false; } enqueue(q, node->left); } else { flag = true; } if (node->right) { if (flag) { return false; } enqueue(q, node->right); } else { flag = true; } } return true; } int main() { TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->val = 2; root->right = (TreeNode *)malloc(sizeof(TreeNode)); root->right->val = 3; root->left->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode *)malloc(sizeof(TreeNode)); root->left->right->val = 5; root->right->left = (TreeNode *)malloc(sizeof(TreeNode)); root->right->left->val = 6; printf("%s\n", isCompleteTree(root) ? "true" : "false"); return 0; } ``` 代码中使用了队列来存储二叉树中的节点,判断是否为完全二叉树的方法是,从根节点开始,每层的节点必须都存在,否则后面的节点都必须是叶子节点才满足完全二叉树的定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值