算法---LeetCode 222. 完全二叉树的节点个数

1. 题目

原题链接

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层
为第 h 层,则该层包含 1~ 2h 个节点。

示例 1:

输入:root = [1,2,3,4,5,6]
输出:6

示例 2:

输入:root = []
输出:0

2. 题解

2.1 解法1: 常规递归

    class Solution {
        public int countNodes(TreeNode root) {
            return dfs(root);
        }

        public int dfs(TreeNode root) {
            if (root == null) {
                return 0;
            }
            return dfs(root.left) + dfs(root.right) + 1;
        }

    }

2.2 解法2: 特殊递归(利用完全二叉树的性质)

来对 root 节点的左右子树进行高度统计,分别记为 left 和 right,有以下两种结果:

  1. left == right。这说明,左子树一定是满二叉树,因为节点已经填充到右子树了,左子树必定已经填满了。所以左子树的节点总数我们可以直接得到,是 2^left - 1,加上当前这个 root 节点,则正好是 2^left。再对右子树进行递归统计。

  2. left != right。说明此时最后一层不满,但倒数第二层已经满了,可以直接得到右子树的节点个数。同理,右子树节点 +root 节点,总数为 2^right。再对左子树进行递归查找。

关于求二叉树的层数

对于所有二叉树, 可以用以下递归来处理

private int countLevel(TreeNode root){
        if(root == null){
            return 0;
        }
        return Math.max(countLevel(root.left),countLevel(root.right)) + 1;
}

但对于完全二叉树, 不用递归, 直接迭代的向左子树寻找即为高度

        public int countLevel(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int h = 0;
            while (root != null) {
                root = root.left;
                h++;
            }
            return h;
        }

递归版本代码:

    class Solution {
        public int countNodes(TreeNode root) {
            return dfs(root);
        }

        public int dfs(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int left = countLevel(root.left);
            int right = countLevel(root.right);
            if (left == right) {
                // 说明左子树是满的
                return dfs(root.right) + (int) Math.pow(2, left);
            } else {
                return dfs(root.left) + (int) Math.pow(2, right);
            }

        }
		// 计算二叉树的高度
        public int countLevel(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int h = 0;
            while (root != null) {
                root = root.left;
                h++;
            }
            return h;
        }


    }

非递归版本:

    class Solution {
        public int countNodes(TreeNode root) {
            if (root == null) return 0;
            int left = countLevel(root.left);
            int count = 0;
            while (root != null) {
                int right = countLevel(root.right);
                if (right == left) {
                    count += (1 << left);
                    root = root.right;
                } else {
                    count += (1 << right);
                    root = root.left;
                }
                left--;
            }
            return count;
        }

        public int countLevel(TreeNode root) {
            if (root == null) return 0;
            int h = 0;
            while (root != null) {
                root = root.left;
                h++;
            }
            return h;
        }
    }

参考: 常规解法和击败100%的Java解法

2.3 解法 3: 二分查找

参考:C++ 三种方法解决完全二叉树的节点个数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值