[GeeksForGeeks] Perfect Binary Tree

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 }

 

以上是关于[GeeksForGeeks] Perfect Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

Perfect Squares

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

UVA - 10622 Perfect P-th Powers

Perfect Keyboard

CF 980D Perfect Groups(数论)

CodeForces947 C. Perfect Security 异或Trie