Blue Bridge Cup: a 10-level binary tree, how many nodes it contains at most (the maximum number of nodes)

topic

[Problem description]
   How many nodes does a 10-level binary tree contain at most?
   Note that when a binary tree has only one node, it is one level.
[Answer Submission]
   This is a question that fills in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. If you fill in the extra content, you will not be able to score.

answer

   1023

Problem solving ideas

  for loop accumulation

Code

public class Main {
    
    //蓝桥杯要求class命名为Main,且无package
    public static void main(String[] args) {
    
    
        int sum=1,num=1;//sum=1,1代表第一层的根节点
        for(int i=1;i<10;i++){
    
    
            num*=2;//num代表i+1层的节点数
            sum+=num;
        }
        System.out.println(sum);
    }
}

Guess you like

Origin blog.csdn.net/qq_47168235/article/details/108911376