当前位置:网站首页>Leetcode 222 number of nodes of complete binary tree
Leetcode 222 number of nodes of complete binary tree
2022-07-05 04:32:00 【baj001】
Ideas
- Method 1 : Using recursion , as follows
- Method 2 : Use the classic solution , Sequence traversal , To count the number of nodes
class Solution {
// General recursive solution
public int countNodes(TreeNode root) {
if(root == null) {
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
// Use iterative algorithm
class Solution {
public int countNodes(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> que = new LinkedList<TreeNode>();
que.add(root);
int num = 0;
while(!que.isEmpty()){
int size = que.size();
while(size > 0){
TreeNode node = que.poll();
if(node.left != null) que.add(node.left);
if(node.right != null) que.add(node.right);
num++;
size--;
}
}
return num;
}
}
边栏推荐
- Wenet: E2E speech recognition tool for industrial implementation
- [Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
- 包 类 包的作用域
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- Neural networks and deep learning Chapter 3: linear model reading questions
- MySQL: view with subquery in the from clause limit
- How to get the first few pieces of data of each group gracefully
- Looking back on 2021, looking forward to 2022 | a year between CSDN and me
- FFmepg使用指南
- 【UNIAPP】系统热更新实现思路
猜你喜欢
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
Interview related high-frequency algorithm test site 3
Pointer function (basic)
Matplotlib draws three-dimensional scatter and surface graphs
How to get the first few pieces of data of each group gracefully
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
MacBook安装postgreSQL+postgis
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
托管式服务网络:云原生时代的应用体系架构进化
函數(易錯)
随机推荐
Introduction to RT thread kernel (5) -- memory management
指针函数(基础)
假设检验——《概率论与数理统计》第八章学习笔记
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
Neural networks and deep learning Chapter 2: machine learning overview reading questions
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
机器学习 --- 神经网络
FFmepg使用指南
Neural network and deep learning Chapter 1: introduction reading questions
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
函数(基本:参数,返回值)
Invalid bound statement (not found) in idea -- problem solving
如何优雅的获取每个分组的前几条数据
Stage experience
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
Scheduling system of kubernetes cluster
Sword finger offer 07 Rebuild binary tree
2022-2028 global and Chinese FPGA prototype system Market Research Report