Full Binary Tree

Posted TCMのBLOG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Full Binary Tree相关的知识,希望对你有一定的参考价值。

Time Limit: 2000MS Memory limit: 65536K
题目描述
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.

输入
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.

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

示例输入

5
1 2
2 3
4 3
1024 2048
3214567 9998877

示例输出

1
2
3
1
44


来源

2014年山东省第五届ACM大学生程序设计竞赛

 

大神的代码竟然是这样:

#include <stdio.h>  
int main()  
{  
    int i,j;  
    int n,m;  
    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        int count=0;  
        scanf("%d %d",&n,&m);  
        while(n!=m)  
        {  
            if(n>m)  
                n/=2;  
            else  
                m/=2;  
            count++;  
        }  
        printf("%d\n",count);  
    }  
    return 0;  
}  

 

佩服佩服 

 

以上是关于Full Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

Full Binary Tree(sdut 2882)

hihocoder 1342 Full Binary Tree Picture完全二叉树

Cracking the Coding Interview, Binary Tree, Binary Search Tress

1159 Structure of a Binary Tree + 根据前序和中序构建二叉树+ 层序遍历模板复习

[LeetCode226]Invert Binary Tree

Binary Tree Traversals ,create binary tree as follow (Figure-1) in computer