算法leetcode222. 完全二叉树的节点个数(rust和go)
Posted 二当家的白帽子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法leetcode222. 完全二叉树的节点个数(rust和go)相关的知识,希望对你有一定的参考价值。
文章目录
- 222. 完全二叉树的节点个数:
- 样例 1:
- 样例 2:
- 样例 3:
- 提示:
- 分析
- 题解
- 原题传送门:https://leetcode.cn/problems/count-complete-tree-nodes/
222. 完全二叉树的节点个数:
给你一棵 完全二叉树 的根节点 root
,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
层,则该层包含 1~ 2h 个节点。
样例 1:
输入:
root = [1,2,3,4,5,6]
输出:
6
样例 2:
输入:
root = []
输出:
0
样例 3:
输入:
root = [1]
输出:
1
提示:
- 树中节点的数目范围是[0, 5 * 104]
- 0 <= Node.val <= 5 * 104
- 题目数据保证输入的树是 完全二叉树
分析
- 面对这道算法题目,二当家的陷入了沉思。
- 直接用递归套娃大法即可。
- 但是题目一再强调是完全二叉树,用递归套娃大法并没有考虑这个条件。
- 其实我们只需要看最底层叶子层的情况,找到叶子节点存在和不存在的临界点。
题解
rust
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
//
//
// impl TreeNode
// #[inline]
// pub fn new(val: i32) -> Self
// TreeNode
// val,
// left: None,
// right: None
//
//
//
use std::rc::Rc;
use std::cell::RefCell;
impl Solution
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32
if root.is_none()
return 0;
let root = root.as_ref().unwrap().borrow();
return 1 + Solution::count_nodes(root.left.clone()) + Solution::count_nodes(root.right.clone());
go
/**
* Definition for a binary tree node.
* type TreeNode struct
* Val int
* Left *TreeNode
* Right *TreeNode
*
*/
func countNodes(root *TreeNode) int
if root == nil
return 0
return 1 + countNodes(root.Left) + countNodes(root.Right)
typescript
/**
* Definition for a binary tree node.
* class TreeNode
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null)
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
*
*
*/
function countNodes(root: TreeNode | null): number
if (!root)
return 0;
return 1+ countNodes(root.left) + countNodes(root.right);
;
python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countNodes(self, root: TreeNode) -> int:
if root is None:
return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
c
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* ;
*/
int countNodes(struct TreeNode* root)
if (root == NULL)
return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
c++
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
int countNodes(TreeNode* root)
if (root == NULL)
return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
;
java
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public int countNodes(TreeNode root)
if (root == null)
return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public int countNodes(TreeNode root)
if (root == null)
return 0;
// 取得二叉树层级
int level = 0;
TreeNode node = root;
while (node.left != null)
level++;
node = node.left;
// 二分查找叶子节点存在和不存在的临界点
int low = 1 << level, high = (1 << (level + 1)) - 1, bits = 1 << (level - 1);
while (low < high)
int mid = (high + low + 1) / 2;
if (exists(root, bits, mid))
low = mid;
else
high = mid - 1;
return low;
private boolean exists(TreeNode root, int bits, int k)
while (root != null && bits > 0)
if ((bits & k) == 0)
root = root.left;
else
root = root.right;
bits >>= 1;
return root != null;
原题传送门:https://leetcode.cn/problems/count-complete-tree-nodes/
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~
以上是关于算法leetcode222. 完全二叉树的节点个数(rust和go)的主要内容,如果未能解决你的问题,请参考以下文章
⭐算法入门⭐《二叉树》简单06 —— LeetCode 222. 完全二叉树的节点个数
[JavaScript 刷题] 树 - 完全二叉树的节点个数, leetcode 222