Complete Tree

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.


CompleteBinary

lc#222 Count Complete Tree Nodes
compute the number of nodes in complete tree

public int countNodes(TreeNode root) {
    if(root == null) return 0;

    int height = 0;
    TreeNode left = root.left, right = root.right;
    while(left != null && right != null) {
        height++; 
        left = left.left;
        right = right.right;
    }
    
    return left == right ? (1 << height + 1) - 1 : 1 + countNodes(root.left) + countNodes(root.right);
} 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,178评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,496评论 0 23
  • 今天是1月20日,阴历的腊月15,大寒,我家宝贝女儿的一周岁生日,今天中午视频后存了照片发到亲宝宝上看到的...
    云钏阅读 1,018评论 0 1
  • 昨夜迷蒙的烟雨竟不经意间湿透了木樨的香 摇曳。妖娆。 望向窗外,近处的楼也似乎被雾雨笼罩了思绪。 变得那么的迷离。...
    而已阅读 1,562评论 0 5

友情链接更多精彩内容