zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Check sum of covered and uncovered nodes of binary tree

    Given a binary tree, you need to check whether sum of all covered elements is equal to sum of all uncovered elements or not.
    In a binary tree, a node is called Uncovered if it appears either on left boundary or right boundary. Rest of the nodes are called covered.

    For example, consider below binary tree

    tree
    In above binary tree,
    Covered node:	 6, 4, 7
    Uncovered node:   8, 3, 1, 10, 14, 13
    
    The output for this tree should be false as 
    sum of covered and uncovered node is not same
    

     1 public class CoverBt {
     2     public static boolean checkSum(TreeNode root) {
     3         if(root == null) {
     4             return true;
     5         }
     6         int sumAll = sum(root);
     7         int sumUncovered = uncoveredSum(root);
     8         return sumUncovered == (sumAll - sumUncovered);
     9     }
    10     private static int sum(TreeNode root) {
    11         if(root == null) {
    12             return 0;
    13         }
    14         return root.val + sum(root.left) + sum(root.right);
    15     }
    16     private static int uncoveredSumLeft(TreeNode node) {
    17         if(node.left == null && node.right == null) {
    18             return node.val;
    19         }
    20         if(node.left != null) {
    21             return node.val + uncoveredSumLeft(node.left);
    22         }
    23         return node.val + uncoveredSumLeft(node.right);
    24     }
    25     private static int uncoveredSumRight(TreeNode node) {
    26         if(node.left == null && node.right == null) {
    27             return node.val;
    28         }
    29         if(node.right != null) {
    30             return node.val + uncoveredSumRight(node.right);
    31         }
    32         return node.val + uncoveredSumRight(node.left);
    33     }
    34     private static int uncoveredSum(TreeNode root) {
    35         int lb = 0, rb = 0;
    36         if(root.left != null) {
    37             lb = uncoveredSumLeft(root.left);
    38         }
    39         if(root.right != null) {
    40             rb = uncoveredSumRight(root.right);
    41         }
    42         return root.val + lb + rb;
    43     }
    44     public static void main(String[] args) {
    45         TreeNode[] nodes = new TreeNode[9];
    46         nodes[0] = new TreeNode(8);
    47         nodes[1] = new TreeNode(3);
    48         nodes[2] = new TreeNode(10);
    49         nodes[3] = new TreeNode(1);
    50         nodes[4] = new TreeNode(6);
    51         nodes[5] = new TreeNode(14);
    52         nodes[6] = new TreeNode(4);
    53         nodes[7] = new TreeNode(7);
    54         nodes[8] = new TreeNode(13);
    55         nodes[0].left = nodes[1]; nodes[0].right = nodes[2];
    56         nodes[1].left = nodes[3]; nodes[1].right = nodes[4];
    57         nodes[2].right = nodes[5];
    58         nodes[4].left = nodes[6]; nodes[4].right = nodes[7];
    59         nodes[5].left = nodes[8];
    60         System.out.println(checkSum(nodes[0]));
    61     }
    62 }

    For calculating sum of Uncovered nodes we will follow below steps:
    1) Start from root, go to left and keep going left as long as left child is available, if not go to right child and again follow same procedure of going left until you reach a leaf node.

    2) After step 1 sum of left boundary will be stored, now for right part again do the same procedure but now keep going to right as long as right child is available, if not then go to left child and follow same procedure of going right until you reach a leaf node.

    After above 2 steps sum of all Uncovered node will be stored, we can subtract it from total sum and get sum of covered elements and check for equines of binary tree.




  • 相关阅读:
    类的继承
    垃圾回收GC
    网络层
    数据链路层
    TCP/IP协议分层模型
    OSI参考模型
    浏览器访问一个域名的过程
    Thread&ThreadLocal
    设计模式---单例模式
    内存泄漏和内存溢出
  • 原文地址:https://www.cnblogs.com/lz87/p/7643662.html
Copyright © 2011-2022 走看看