当前位置:网站首页>Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
2022-07-05 19:59:00 【Taotao can't learn English】
222. The number of nodes in a complete binary tree
Give a completely binary tree , Find out the number of nodes of the tree .
Example 1:
- Input :root = [1,2,3,4,5,6]
- Output :6
Example 2:
- Input :root = []
- Output :0
Example 3:
- Input :root = [1]
- Output :1
Tips :
- The number of nodes in the tree ranges from [0, 5 * 10^4]
- 0 <= Node.val <= 5 * 10^4
- The topic data ensures that the input tree is Perfect binary tree
Direct sequence traversal search , It can be regarded as a happy method .
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.Deque;
/** * @ClassName CountNodes * @Descriotion TODO * @Author nitaotao * @Date 2022/7/4 10:15 * @Version 1.0 * https://leetcode.cn/problems/count-complete-tree-nodes/ * 222. The number of nodes in a complete binary tree **/
public class CountNodes {
public int countNodes(TreeNode root) {
/** * Sequence traversal */
if (root == null) {
return 0;
}
Deque<TreeNode> deque = new ArrayDeque<TreeNode>();
deque.offer(root);
int sum = 0;
while (!deque.isEmpty()) {
int size = deque.size();
while (size > 0) {
root = deque.pop();
sum++;
if (root.left != null) {
deque.offer(root.left);
}
if (root.right != null) {
deque.offer(root.right);
}
size--;
}
}
return sum;
}
}

According to the properties of full binary tree , Number of nodes = front n-1 Number of layer nodes + Number of nodes in the last layer
front n-1 Number of layer nodes = 2^(n-1) -1
/** * According to the properties of complete binary tree * * @param root * @return */
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if (leftDepth == rightDepth) {
// If the depth is equal , Then the left side is full of binary trees
// leftDepth Is to calculate the depth from the head node of the current subtree , Not the root node of two trees
// ( 2 ^ leftDepth - 1 ) + 1 1 It is the root node of two trees
return ((1 << leftDepth) - 1) + 1 + countNodes(root.right);
} else {
// The right subtree is a full binary tree
return ((1 << rightDepth) - 1) + 1 + countNodes(root.left);
}
}
/** * From current subtree Start to calculate the depth * * @param root * @return */
public int getDepth(TreeNode root) {
int depth = 0;
while (root != null) {
root = root.left;
depth++;
}
return depth;
}

边栏推荐
- 解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
- 淺淺的談一下ThreadLocalInsecureRandom
- C language OJ gets PE, OJ of ACM introduction~
- 浅浅的谈一下ThreadLocalInsecureRandom
- leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
- 浮动元素与父级、兄弟盒子的关系
- Tasks in GStreamer
- 【c语言】归并排序
- Information / data
猜你喜欢

四万字长文说operator new & operator delete

建立自己的网站(16)

微信小程序正则表达式提取链接

Elk distributed log analysis system deployment (Huawei cloud)

解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables

Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测

Do you know several assertion methods commonly used by JMeter?

IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times

Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
随机推荐
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
挖财钱堂教育靠谱安全吗?
浅浅的谈一下ThreadLocalInsecureRandom
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
okcc呼叫中心有什么作用
Go language | 03 array, pointer, slice usage
Go language | 02 for loop and the use of common functions
Xaas trap: all things serve (possible) is not what it really needs
众昂矿业:2022年全球萤石行业市场供给现状分析
【obs】libobs-winrt :CreateDispatcherQueueController
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
What are general items
JVMRandom不可设置种子|问题追溯|源码追溯
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
c——顺序结构
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
How to select the Block Editor? Impression notes verse, notation, flowus