当前位置:网站首页>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;
}

边栏推荐
- Information / data
- leetcode刷题:二叉树10(完全二叉树的节点个数)
- Elk distributed log analysis system deployment (Huawei cloud)
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- ffplay文档[通俗易懂]
- Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
- Process file and directory names
- Parler de threadlocal insecurerandom
- Flume series: interceptor filtering data
- Android interview classic, 2022 Android interview written examination summary
猜你喜欢

Using repositoryprovider to simplify the value passing of parent-child components

深度学习 卷积神经网络(CNN)基础

Recommended collection, my Tencent Android interview experience sharing

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

ACM getting started Day1

Database logic processing function

leetcode刷题:二叉树12(二叉树的所有路径)

Worthy of being a boss, byte Daniel spent eight months on another masterpiece

【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*

众昂矿业:2022年全球萤石行业市场供给现状分析
随机推荐
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
秋招字节面试官问你还有什么问题?其实你已经踩雷了
Add data to excel small and medium-sized cases through poi
多分支结构
gst-launch的-v参数
Concept and syntax of function
leetcode刷题:二叉树15(找树左下角的值)
四万字长文说operator new & operator delete
What is PyC file
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Do you know several assertion methods commonly used by JMeter?
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
40000 word Wenshuo operator new & operator delete
Android interview classic, 2022 Android interview written examination summary
BZOJ 3747 POI2015 Kinoman 段树
MySql的root密码忘记该怎么找回
Is it safe to open a mobile stock account? Is it reliable?