博客
关于我
Codeup——611 | 问题 B: 二叉树
阅读量:162 次
发布时间:2019-02-26

本文共 1318 字,大约阅读时间需要 4 分钟。

为了解决这个问题,我们需要计算结点m所在的子树中包括多少个结点。已知二叉树的构造规则是每个结点的左子树是其两倍,右子树是其两倍加一。

方法思路

我们可以使用一种高效的方法来解决这个问题。具体步骤如下:

  • 问题分析:我们需要找到结点m的子树中的所有结点数目,直到结点不超过n。每个结点k的左子树是2k,右子树是2k+1。

  • 队列处理:我们可以使用队列来处理每个结点的左子树和右子树节点,直到队列为空或者所有节点超过n。

  • 计数节点:每次从队列中取出一个节点,如果它不超过n,计数加一,并将其左子树和右子树节点加入队列(如果它们不超过n)。

  • 这种方法的时间复杂度是O(log n),因为每次处理一个节点时,节点值至少翻倍,因此在最坏情况下也只需要log n次迭代。

    解决代码

    import sysfrom collections import dequedef main():    for line in sys.stdin:        line = line.strip()        if not line:            continue        parts = line.split()        if len(parts) == 2:            m, n = map(int, parts)            if m == 0 and n == 0:                break            if n < m:                print(0)                continue            count = 0            q = deque()            q.append(m)            while q:                current = q.popleft()                if current > n:                    continue                count += 1                left = current * 2                right = current * 2 + 1                if left <= n:                    q.append(left)                if right <= n:                    q.append(right)            print(count)if __name__ == "__main__":    main()

    代码解释

  • 读取输入:从标准输入读取数据,处理每行的m和n,直到遇到0 0。

  • 初始化队列:将起始结点m加入队列。

  • 处理队列:循环处理队列中的每个节点。如果节点值超过n,跳过。否则,计数加一,并将其左子树和右子树节点加入队列(如果不超过n)。

  • 这种方法确保了在处理大数时的效率,并且能够正确计算结点m所在子树中的结点数目。

    转载地址:http://npr.baihongyu.com/

    你可能感兴趣的文章
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>