222完全二叉树的节点个数

来源:https://leetcode-cn.com/problems/count-complete-tree-nodes/

法一: 自己的代码

思路: 刚开始的方法多遍历了一行,改进后的,只需遍历到最后一行就停,这是由完全二叉树的性质决定的,注意层序遍历二叉树的时候,这里不能用栈来实现,必须用队列,才能保证每一行都是从左往右来遍历,遍历最后一行的时候才不会出错

# 执行用时 :124 ms, 在所有 python3 提交中击败了29.46% 的用户
# 内存消耗 :20 MB, 在所有 python3 提交中击败了98.68%的用户
class Solution:
    def countNodes(self, root: TreeNode) -> int:
        stack = []
        stack_next = []
        level = 0
        if root is None:
            return 0
        stack_next.append(root)
        while stack_next:
            stack = stack_next
            stack_next = []
            l = len(stack)
            # 非空的list才能pop()
            while stack:
                # 注意这里用的是队列不是栈,为的是保证每次都是从左往右遍历
                a = stack.pop(0)
                if a:
                    stack_next.append(a.right)
                    stack_next.append(a.left)
                # 一旦遇到某个节点为空了,说明到最后一行了,直接计算并返回结果
                else:
                    return int(len(stack_next)/2) + (2 ** (level-1)) - 1
            level += 1
        # 原先的代码比较耗时,最后一行可能会无效遍历,
        # return (2 ** (level-2) - 1 + int(l/2))

原文地址:https://www.cnblogs.com/xxswkl/p/12003276.html

时间: 2024-08-30 16:41:22

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

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

完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h 层,则该层包含 1~ 2h 个节点. 示例: 输入: 1 / \ 2 3 / \ / 4 5 6 输出: 6 1 public class Solution { 2 3 // 获取左子树的高度(其实是最左侧分支) 4 public int getLeftHeight

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

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

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=

二叉树节点个数,叶子个数,第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)   /

kAri OJ 92 统计节点个数 邻接表

92. 统计节点个数 时间限制 1000 ms 内存限制 65536 KB 题目描述 给出一棵有向树,一共有N(1<N≤1000)个节点,如果一个节点的度(入度+出度)不小于它所有儿子以及它父亲的度(如果存在父亲或儿子),那么我们称这个节点为p节点,现在你的任务是统计p节点的个数. 如样例,第一组的p节点为1,2,3:第二组的p节点为0. 输入格式 第一行为数据组数T(1≤T≤100). 每组数据第一行为N表示树的节点数.后面为N?1行,每行两个数x,y(0≤x,y<N),代表y是x的儿子节点