Leetcode 222.完全二叉树的节点个数

完全二叉树的节点个数

给出一个完全二叉树,求出该树的节点个数。

说明:

完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

示例:

输入:

1

/ \

2 3

/ \ /

4 5 6

输出: 6

 1 public class Solution {
 2
 3     // 获取左子树的高度(其实是最左侧分支)
 4     public int getLeftHeight(TreeNode root) {
 5         int count = 0;
 6         while (root != null) {
 7             count++;
 8             root = root.left;
 9         }
10         return count;
11     }
12
13     // 获取右子树的高度(其实是最右侧分支的高度)
14     public int getRightHeight(TreeNode root) {
15         int count = 0;
16         while (root != null) {
17             count++;
18             root = root.right;
19         }
20         return count;
21     }
22
23     public int countNodes(TreeNode root) {
24         if (root == null) {
25             return 0;
26         }
27         int leftHeight = getLeftHeight(root);
28         int rightHeight = getRightHeight(root);
29
30         if (leftHeight == rightHeight) {
31             // 表示是满二叉树,二叉树的节点数直接由公式2^n-1得到
32             // leftHeight即为层数, 1 << leftHeight使用位运算计算2^leftHeight,效率更高
33             // 注意(1 << leftHeight) - 1 的括号必须有!!
34             return (1 << leftHeight) - 1;
35         } else {
36             // 若该二叉树不是满二叉树,递归的调用该方法,计算左子树和右子树的节点数
37             return countNodes(root.left) + countNodes(root.right) + 1;
38         }
39     }
40 }

原文地址:https://www.cnblogs.com/kexinxin/p/10203064.html

时间: 2024-08-06 07:59:20

Leetcode 222.完全二叉树的节点个数的相关文章

LeetCode 222. 完全二叉树的节点个数(Count Complete Tree Nodes)

题目描述 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h 层,则该层包含 1~ 2h 个节点. 示例: 输入: 1 / 2 3 / \ / 4 5 6 输出: 6 解题思路 从根节点开始分别判断左右子树的高度: 若左子树高度等于右子树,说明左子树一定为满二叉树,可得左子树的总节点个数,然后递归求右子树的节点数: 若左子树高度大于右子树

222完全二叉树的节点个数

来源:https://leetcode-cn.com/problems/count-complete-tree-nodes/ 法一: 自己的代码 思路: 刚开始的方法多遍历了一行,改进后的,只需遍历到最后一行就停,这是由完全二叉树的性质决定的,注意层序遍历二叉树的时候,这里不能用栈来实现,必须用队列,才能保证每一行都是从左往右来遍历,遍历最后一行的时候才不会出错 # 执行用时 :124 ms, 在所有 python3 提交中击败了29.46% 的用户 # 内存消耗 :20 MB, 在所有 pyt

222. 完全二叉树的节点个数

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution 11 { 12 vector<int> ans; 13 public: 14 in

[Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left

[LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as po

二叉树节点个数题目[n0,n1,n2]

若完全二叉树的节点个数为2N-1,则叶节点个数为()    A)N-1        B)2×N        C)2N-1        D)2N解析:    结点拥有的子树数为结点的度    证明:因为二叉树中所有结点的度数均不大于2,所以结点总数(记为n)应等于0度结点数.1度结点(记为n1)和2度结点数之和:                                                                                         n=

leetcode 222 Count Complete Tree Nodes (计算完全二叉树节点数)

1. 问题描述 计算完全二叉树的节点数.对于完全二叉树的定义可参考wikipedia上面的内容. 2. 方法与思路 最简单也最容易想到的方法就是使用递归,分别递归计算左右子树的节点数的和.但此方法最容易超时,一般不可取. int countNodes(TreeNode* root) { if(root == NULL) return 0; else if(root->left == NULL) return 1; return countNodes(root->left) + countNod

二叉树节点个数,叶子个数,第K层个数,最低公共节点

1. 节点个数 function getNodeNum(root){ if(root == null){ return 0; } //+1为root的计数 return getNodeNum(root.left) + getNodeNum(root.right) + 1; } 2. 叶子个数 function getLeafNum(root){ if(root == null){ return 0; } if(root.left == null && root.right == null)

先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)

#include "stdio.h" #include "string.h" #include "malloc.h" #define NULL 0 #define MAXSIZE 30 typedef struct BiTNode      //定义二叉树数据结构 { char data; struct BiTNode *lchild,*rchild; } BiTNode; void preCreate(BiTNode *& T)   /