• 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 possible. It can have between 1 and 2h nodes inclusive at the last level h.
    数二叉树的节点数首先肯定是可以用暴力解法去解问题的,但是这样总是会timeout:

     1 class Solution {
     2 public:
     3     int countNodes(TreeNode* root) {
     4         if(!root) return 0;
     5         total++;
     6         countNodes(root->left);
     7         countNodes(root->right);
     8         return total;
     9     }   
    10 private:
    11     int total;
    12 };

    其他的办法就是对完全二叉树而言,其一直向左走和一直向右边、走只可能是相同的或者相差1,如果相同那么起节点个数就是2^h - 1否曾再递归的加上左右节点的和就可以了。

     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) return 0;
    14         TreeNode * leftNode = root;
    15         TreeNode * rightNode = root;
    16         int left = 0;
    17         int right = 0;
    18         while(leftNode->left){
    19             left++;
    20             leftNode=leftNode->left;
    21         }
    22         while(rightNode->right){
    23             right++;
    24             rightNode = rightNode->right;
    25         }
    26         if(left == right) return (1 << (left + 1)) - 1;
    27         else return 1 + countNodes(root->left) + countNodes(root->right);
    28     }
    29 };

    写的比较乱哈 ,凑合着看看把。

    下面是java代码,首先肯定是递归形式的,不出所料,同样会TLE:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int countNodes(TreeNode root) {
    12         if(root == null)
    13             return 0;
    14         else
    15             return 1 + countNodes(root.left) + countNodes(root.right);       
    16     }
    17 }

    考虑到完全二叉树的性质,下面这个写法就不会TLE了:

     1 public class Solution {
     2     public int countNodes(TreeNode root) {
     3         if(root == null)
     4             return 0;
     5         int right = 1, left = 1;
     6         TreeNode leftNode = root;
     7         TreeNode rightNode = root;
     8         while(leftNode.left != null){
     9             leftNode = leftNode.left;
    10             left++;
    11         }
    12         while(rightNode.right != null){
    13             rightNode = rightNode.right;
    14             right++;
    15         }
    16         if(left == right)
    17             return (1 << left) - 1;
    18         else
    19             return 1 + countNodes(root.left) + countNodes(root.right); //注意这里不要忘记计算本身的这个节点
    20     }
    21 }
  • 相关阅读:
    bzoj 4008 亚瑟王 期望概率dp
    t[..., 1, tf.newaxis]
    Keras learning_phase()和learning_phase_scope()
    Keras Sequential模型和add()
    Keras克隆层
    Keras搭建一个Wide & Deep 神经网络
    1 链表的数据结构
    海康威视2017软件精英挑战赛初赛题目
    2016年倒计时两天
    可自定义片头的腾讯视频无广告可全屏调用代码
  • 原文地址:https://www.cnblogs.com/-wang-cheng/p/4908754.html
Copyright © 2020-2023  润新知