[Leetcode] Detailed Explanation of Daily Featured Questions Part 222. Number of Nodes in Complete Binary Tree

        Hi, everyone, I’m Chef Yuan (because I love cooking, I got my chef certificate). I have been reading blogs written by everyone before and learned a lot. Then recently I have developed an idea I wrote, and I will share what I know with the students in need. In the future, I will share various solutions of Leetcode's selected topics and some small Demos of Python, JS, JQ, CSS, PHP, JAVA every day. Please pay attention to me and exchange and study together.


Title description

Insert picture description here


Recursive solution

Question ideas

The first idea to see this problem is to use recursion. This problem is the same as finding the maximum depth , but the maximum depth is to return the maximum value. Our problem is to return the sum. For the recursive trilogy mentioned in the previous article, we will put the title on top of the trilogy. Then find the number of nodes, and when we use recursion, we don’t need to consider too many layers. This will make your brain freeze, and your brain can install a few layers. We only need to consider the number of layers in front of you, and then call recursion. can.

Topic code

class Solution {
    
    
    public int countNodes(TreeNode root) {
    
    
          return  nodeNum(root);           
    }
    //确定递归函数的参数和返回值
    public int nodeNum(TreeNode root){
    
    
       //确定终止条件
        if(root == null){
    
    
            return 0;
        }
        //确定单层条件
        int leftnum = nodeNum(root.left);
        int rightnum = nodeNum(root.right);
        //这里和最大深数是不一样的,我们需要返回的是节点和,
        //left代表的是左子树的节点个数,right代表的是右子树个数,
        //你可以只考虑前两层进行思考,这样比较容易相通
        return leftnum+rightnum+1
    }
}

        


Iterative method (BFS)

Question ideas

Iterative is easier to understand, but it consumes more time and memory. The general principle is that we use queues to push nodes into the queue layer by layer, and then dequeue to count the number. The idea is very simple, and the code implementation is also very simple.

Topic code

class Solution {
    
    
    public int countNodes(TreeNode root) {
    
    
       if(root == null){
    
    
           return 0;
       }
       //创建队列
       Queue<TreeNode> quene = new LinkedList<TreeNode>();
       //头节点入队
       quene.offer(root);
       int count = 0;
       while(!quene.isEmpty()){
    
    
           int size = quene.size();
           //统计节点数目
           count+=size;
           for(int i = 0;i<size;i++){
    
    
               //出队
               TreeNode p = quene.poll();
               TreeNode left = p.left;
               TreeNode right = p.right;
               if(left!=null){
    
    
                   quene.offer(left);
               }
               if(right!=null){
    
    
                   quene.offer(right);
               }
           }
        }
       return count;
    }
}

Fully Binary Tree Characteristic Method

Question ideas

The definition of a complete binary tree: it is an empty tree or its leaf nodes are only in the last two layers. If the last layer is dissatisfied, the leaf nodes are only on the leftmost side. A
complete binary tree is known when we learn about data structures. If the binary tree is full, the number of nodes is 2^h-1; h represents the height of the binary tree. If it is not full, you need to find the number of nodes in the first h-1 layer through the formula, and add the last The number of nodes in a layer.
The main idea of ​​the algorithm is to first obtain the heights of the left subtree and the right subtree respectively, and then determine whether the binary tree is full, and then discuss the two cases. If it is full, find the left subtree first, and then recursively calculate the number of right subtrees. The non-full binary tree first finds the number of right subtrees, and then recursively finds the number of left subtrees. I learned this method from a big guy. If it’s not easy to understand, you can click on this website to view the big guy’s instructions. Big brother explain

Topic code

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){
    
    
           //这里的含义是1向左移动left位,则为2^left次方
            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;
    }
}

to sum up

When I saw this topic, I thought of two methods. I also thought of the third method, but I don’t know how to implement it. But isn’t it an improvement to learn how to solve the problem by others?

Author: LeetCode
link: https: //leetcode-cn.com/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode/
Source: stay button (LeetCode)
copyright reserved by the authors. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/tan45du_yuan/article/details/108926551