当前位置:网站首页>leetcode 222. Number of nodes of a complete binary tree (required)
leetcode 222. Number of nodes of a complete binary tree (required)
2022-07-19 03:29:00 【only-qi】
Problem description :
Give a completely binary tree , Find out the number of nodes of the tree .
explain :
The definition of a complete binary tree is as follows : In a complete binary tree , Except that the lowest node may not be full , The number of nodes in each layer reaches the maximum , And the nodes of the lowest layer are concentrated in the leftmost positions of the layer . If the bottom layer is No h layer , Then the layer contains 1~ 2h Nodes .
Example :
Input :
1
/ \
2 3
/ \ /
4 5 6
Output : 6
Ideas :
Find the number of nodes of a complete binary tree
Code up , Take it to run :
package com.onlyqi.test03.erfen;
/**
* @author onlyqi
* @date 2022/7/12 14:44
* @description
*/
public class Test6 {
public static Integer num =0;
public static void main(String[] args) {
TreeNode treeNode1 = new TreeNode(1);
TreeNode treeNode2 = new TreeNode(2);
TreeNode treeNode3 = new TreeNode(3);
TreeNode treeNode4 = new TreeNode(4);
TreeNode treeNode5 = new TreeNode(5);
TreeNode treeNode6 = new TreeNode(6);
treeNode2.setLeft(treeNode6);
treeNode3.setLeft(treeNode4);
treeNode3.setRight(treeNode5);
treeNode1.setLeft(treeNode3);
treeNode1.setRight(treeNode2);
System.out.println("========================"+count(treeNode1));
}
public static Integer count(TreeNode treeNode) {
if (treeNode != null) {
// Access the root node
num=num+1;
// Access the left node
count(treeNode.getLeft());
// Access the right node
count(treeNode.getRight());
}
return num;
}
}
class TreeNode {
private Integer value;
private TreeNode left;
private TreeNode right;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public TreeNode(Integer value) {
this.value = value;
}
}Running results :

I want to brush it 300 Algorithm questions , The first 106 Avenue
边栏推荐
- [MCU simulation] (XIV) instruction system bit operation instructions - bit data transmission instructions MOV, bit variable modification instructions
- Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
- Yolov5 opencv DNN reasoning
- Chengxin University envi_ IDL second week homework: extract aerosol thickness at n points + detailed analysis
- Pytorch best practices and code templates
- SQL classic exercises (x30)
- Go语言中的Iota关键字怎么使用
- Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]
- The WinRAR command copies the specified folder as a compressed file, and calls the scheduled task for backup.
- Powertor500t reports an error 0x01806803
猜你喜欢

Solve the error of 0x00000709 when win10 connects to the shared printer

374. Guess the size of numbers (must be able to get started)

Leetcode: subsequence problem in dynamic programming
![mysqldump: [Warning] Using a password on the command line interface can be insecure.](/img/91/8b0d35f85bc0f46daac4e1e9bc9e34.png)
mysqldump: [Warning] Using a password on the command line interface can be insecure.

Use RZ, SZ commands to upload and download files through xshell7
![[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th](/img/fd/f0c0e17f2942525a78fbd443514992.png)
[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th

Wdog and power mode of fs32k148 commissioning

ES6学习笔记——B站小马哥

Chengxin University envi_ IDL first week experiment test: simple operation of array + detailed analysis

About 1000base-t1 1000Base-TX and 100base-t1
随机推荐
CorelDRAW cannot be installed. Solution
MYSQL主从搭建
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
Bisenetv1 face segmentation
Dive into deep learning - 2.2 data preprocessing
ES6学习笔记——B站小马哥
Net SNMP related commands
leetcode:50. Pow(x, n)
oracle 查询非自增长分区的最大分区
05 central processing unit
Theoretical basis of double Q-learning and its code implementation [pendulum-v0]
MySQL multi table query
Using gatekeeper to restrict kubernetes to create specific types of resources
自动装配 & 集合注入
[MCU simulation] (XIV) instruction system bit operation instructions - bit data transmission instructions MOV, bit variable modification instructions
The third day of the three questions of Luogu daily (make up on the fourth day)
Visual analysis of ncnn param file and bin model
IEEE754 standard floating point format
Chengxin University envi_ IDL first week experiment test: simple operation of array + detailed analysis
Wechat applet -- Summary of problems in the actual development of taro framework