LeetCode之完全二叉树的节点个数

本文介绍了四种不同的方法来计算完全二叉树的节点数量:迭代深度优先搜索、递归深度优先搜索、二分查找结合位运算以及位移运算。每种方法都提供了详细的代码实现,包括迭代和递归的思路,以及利用位运算提高效率的策略。这些算法对于理解和操作二叉树结构具有重要意义。
摘要由CSDN通过智能技术生成

题目: 给出一个完全二叉树,求出该树的节点个数。
说明:
完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
示例:
在这里插入图片描述
方法一:迭代深度优先搜索

/*
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int countNodes(TreeNode root) {
        int count=1;
		Stack<TreeNode> stack=new Stack<>();
		if(root==null)return 0;
        TreeNode root1=root;
		stack.add(root1);
		TreeNode tmp=root1;
		while(!stack.isEmpty()||tmp.left!=null||tmp.right!=null) {
			if(tmp.left!=null) {//左子树不为空
				count++;
				stack.add(tmp.left);
				tmp=tmp.left;
			}else {//左子树为空,查看右子树
				//tmp=tmp.right;
				if(tmp.right!=null) {
					count++;
					stack.add(tmp.right);
					tmp=tmp.right;
				}else {//右子树也为空
					TreeNode t=stack.pop();
                    if(!stack.isEmpty()){
                        tmp=stack.peek();
                    if(tmp.left==t){
                        tmp.left=null;
                    }
                    if(tmp.right==t){
                        tmp.right=null;
                    }
                }             
				}
			}
		}
		return count;
    }
}

方法二:递归深度优先搜索

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

方法三:二分查找+位运算

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int level = 0;
        TreeNode node = root;
        while (node.left != null) {
            level++;
            node = node.left;
        }
        int low = 1 << level, high = (1 << (level + 1)) - 1;
        while (low < high) {
            int mid = (high - low + 1) / 2 + low;
            if (exists(root, level, mid)) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        return low;
    }

    public boolean exists(TreeNode root, int level, int k) {
        int bits = 1 << (level - 1);
        TreeNode node = root;
        while (node != null && bits > 0) {
            if ((bits & k) == 0) {
                node = node.left;
            } else {
                node = node.right;
            }
            bits >>= 1;
        }
        return node != null;
    }
}

方法四:位移运算

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int countNodes(TreeNode root) {
        if(root == null){
           return 0;
        } 
        int left = countLevel(root.left);
        int right = countLevel(root.right);
        if(left == right){
            return countNodes(root.right) + (1<<left);
        }else{
            return countNodes(root.left) + (1<<right);
        }
    }
    private int countLevel(TreeNode root){
        int level = 0;
        while(root != null){
            level++;
            root = root.left;
        }
        return level;
    }
}

方法五:位运算非递归

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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值