zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Perfect Binary Tree

    Given a Binary Tree, write a function to check whether the given Binary Tree is a prefect Binary Tree or not. A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level.

     1 class SubTreeInfo {
     2     boolean isPerfect;
     3     int height;
     4     SubTreeInfo(boolean isPerfect, int height) {
     5         this.isPerfect = isPerfect;
     6         this.height = height;
     7     }
     8 }
     9 public class PerfectBinaryTree {
    10     public static boolean isPerfectBt(TreeNode root) {
    11         return helper(root).isPerfect;
    12     }
    13     private static SubTreeInfo helper(TreeNode node) {
    14         SubTreeInfo info = new SubTreeInfo(true, 0);
    15         if(node == null) {
    16             return info;
    17         }
    18         SubTreeInfo leftInfo = helper(node.left);
    19         SubTreeInfo rightInfo = helper(node.right);
    20         if(leftInfo.isPerfect && rightInfo.isPerfect && leftInfo.height == rightInfo.height) {
    21             info.height = 1 + leftInfo.height;
    22         }
    23         else {
    24             info.isPerfect = false;
    25         }
    26         return info;
    27     }
    28 }
  • 相关阅读:
    hdu 1047 Integer Inquiry
    大数模板(高精度)
    git 学习
    java List 排序
    简单排序总结
    JDK1.8
    webservice 学习笔记 1
    inline-block,inline,block,table-cell,float
    mybatis 学习视频总结记录
    left join right inner join 区别
  • 原文地址:https://www.cnblogs.com/lz87/p/7643801.html
Copyright © 2011-2022 走看看