算算完全二叉树的节点数4887王中王鉄算盘奖结果

来源:http://www.smjxgs.com 作者:王中王鉄算盘 人气:167 发布时间:2019-08-29
摘要:LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解 题目:   Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every l

LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解

题目:

 

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 possible. It can have between 1 and 2h nodes inclusive at the last level h.

解题:

如果用常规的解法一个个遍历,就是O(n)时间复杂度 ,会不通过,这边就不写O(n)的代码了。因为是完全二叉树,满二叉树有一个性质是节点数等于2^h-1,h为高度,所以可以这样判断节点的左右高度是不是一样,如果是一样说明是满二叉树,就可以用刚才的公式,如果左右不相等就递归计算左右节点。

代码:

 

public static int countNodes(TreeNode root) {
   if(root==null)
    return 0;
   else {
   int left=getLeftHeight(root);
   int right=getRightHeight(root);
   if(left==right)
    return (1<  

Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解 题目: Given a complete binary tree, count the number of nodes. Definition of a complete bina...

LeetCode -- Count Complete Tree Node

题目描述:

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 possible. It can have between 1 and 2h nodes inclusive at the last level h.

统计一个完全树的节点数。

完全树的定义是,高度为h 1的树,前h层的每层节点全满。对于第h 1层,没有叶子的节点均在右侧。意味着,不存在左边有些节点没有叶子而右边有些节点有叶子的情况。

实现思路:
递归求左右高度,如果高度相等,直接返回 Math.Pow(2, h) - 1;如果不等,递归求左子树节点数 递归求右子树的节点数 1;

实现代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {

 public int CountNodes(TreeNode root) 
 {
 if(root == null)
 {
  return 0;
 }

 var leftH = LeftDepth(root.left);
 var rightH = RightDepth(root.right);


 if(leftH == rightH){
  return (int)Math.Pow(2, leftH   1) - 1;
 }
 else{
  return CountNodes(root.left)   CountNodes(root.right)   1;
 }
 }
    public int LeftDepth(TreeNode node){
  var h = 0;
  while(node != null){
   node = node.left;
   h  ;
  }
  return h;
    }

 public int RightDepth(TreeNode node){
  var h = 0;
  while(node != null){
   node = node.right;
   h  ;
  }
  return h;
 }

}

 

-- Count Complete Tree Node 题目描述: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete bin...

Given a complete binary tree, count the number of nodes.

4887王中王鉄算盘奖结果,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 possible. It can have between 1 and 2h nodes inclusive at the last level h.

计算完全二叉树的节点数,如果直接遍历并计算的话会是O(n)的时间复杂度。
能不能充分利用完全二叉树的特性呢
如果从一个根节点开始一直向左和一直向右的深度是相等的,那么意味着这个根节点下面的数是满二叉树,其节点数直接可以算出来。
我们从根节点开始检查,如果不是满二叉树就往下递归,每找到一颗满二叉树就直接返回节点数。

var countNodes = function(root) {
    //获取二叉树一直向左或向右的高度
    var getHeight = function(root,flag) {
        var count = 0;
        if (flag) {
            while (root.left) {
                count  ;
                root = root.left;
            }
        } else {
            while (root.right) {
                count  ;
                root = root.right;
            }
        }
        return count; 
    };
    if (!root) 
        return 0;
    var l = getHeight(root,true);
    var r = getHeight(root,false);
    if (l===r) {
        //如果左右高度相等,节点数等于2^树高-1
        return (2<<l) - 1;
    } else {
        //如果不等就递归
        return countNodes(root.left)   countNodes(root.right)   1;
    }
};

本文由4887王中王鉄算盘奖结果发布于王中王鉄算盘,转载请注明出处:算算完全二叉树的节点数4887王中王鉄算盘奖结果

关键词:

最火资讯