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

题目描述

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

说明:

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

示例:

输入:
    1
   /   2   3
 / \  /
4  5 6

输出: 6

解题思路

从根节点开始分别判断左右子树的高度:

  • 若左子树高度等于右子树,说明左子树一定为满二叉树,可得左子树的总节点个数,然后递归求右子树的节点数;
  • 若左子树高度大于右子树,说明右子树一定为满二叉树,可得右子树的总节点个数,然后递归求左子树的节点数。

代码

 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 public:
12     int countNodes(TreeNode* root) {
13         if(root == NULL) return 0;
14         int left = height(root->left), right = height(root->right);
15         if(left == right)
16             return (1 << left) + countNodes(root->right);
17         else
18             return countNodes(root->left) + (1 << right);
19     }
20     int height(TreeNode* root){
21         if(root == NULL)
22             return 0;
23         return height(root->left) + 1;
24     }
25 };

原文地址:https://www.cnblogs.com/wmx24/p/10273403.html

时间: 2024-08-12 16:38:30

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

[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 222_完全二叉树_遍历】Count Complete Tree Nodes

解法一:递归 int countNodes(TreeNode* root) { if (root == NULL) return 0; TreeNode *pLeft = root->left; TreeNode *pRight = root->right; int ldepth = 0, rdepth = 0; while (pLeft) { ldepth++; pLeft = pLeft->left; } while (pRight) { rdepth++; pRight = pRi

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

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 le

8.8 LeetCode 222 Count Complete Tree Nodes

Question: Count Complete Tree Nodes Total Accepted: 11040 Total Submissions: 53992My Submissions Question Solution Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree

[LeetCode][JavaScript]Count Complete Tree Nodes

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 le

[LeetCode-JAVA] 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

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

LeetCode OJ: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 pos