当前位置:网站首页>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
边栏推荐
- ES6學習筆記——B站小馬哥
- Need to slow down a little
- Using gatekeeper to restrict kubernetes to create specific types of resources
- Ncnn allocator memory allocator
- Bisenetv2 face segmentation ncnn reasoning
- Wechat applet -- Summary of problems in the actual development of taro framework
- oracle 查询 主机名和对应的IP地址
- 无线用的鉴权代码
- Dive Into Deep Learning——2.2数据预处理
- ES6学习笔记——B站小马哥
猜你喜欢

代理模式——B站动力节点
![[template record] string hash to judge palindrome string](/img/90/43109c6bd008990fa5b8147e629685.png)
[template record] string hash to judge palindrome string

Display zabbix6.0 information using grafana8.5.2

Bisenetv2 face segmentation

Powertor500t reports an error 0x01806803

Snapshot: data snapshot (data disclosure method)

Wechat applet -- Summary of problems in the actual development of taro framework

Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis

zsh: command not found: mysql

zsh: command not found: mysql
随机推荐
Zabbix6.0 monitoring vcenter7.0
Le cinquième jour de trois questions par jour à luogu
Pytorch best practices and code templates
Rtx3090 installing pytorch3d
374. Guess the size of numbers (must be able to get started)
Yolov5 ncnn reasoning
数据源对象管理(第三方对象资源) & 加载properties文件
Envi: (the most detailed tutorial in 2022) custom coordinate system
洛谷每日三题之第四天
367. 有效的完全平方数(入门必会)
Snapshot: data snapshot (data disclosure method)
Replacement operation not supported by ncnn partial operators
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
Vscode+ros2 environment configuration
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
KubeCon + CloudNativeCon Europe 2022
Ncnn mat matrix class
Wechat applet -- Summary of problems in the actual development of taro framework
Multi table query - case exercise
Monte Carlo based reinforcement learning method [with code implementation]