Leetcode 222: 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.

 

 

  1 /**
  2  * Definition for a binary tree node.
  3  * public class TreeNode {
  4  *     public int val;
  5  *     public TreeNode left;
  6  *     public TreeNode right;
  7  *     public TreeNode(int x) { val = x; }
  8  * }
  9  */
 10 
 11 public class Solution {
 12     private int Height(TreeNode node)
 13     {
 14         return node == null ? 0 : 1 + Height(node.left);
 15     }
 16     
 17     public int CountNodes(TreeNode root) {
 18         int total = 0;
 19         
 20         while (root != null)
 21         {
 22             int leftHeight = Height(root.left), rightHeight = Height(root.right);
 23             if (leftHeight == rightHeight)
 24             {
 25                 total += (1 << leftHeight);
 26                 root = root.right;
 27             }
 28             else
 29             {
 30                 total += (1 << rightHeight);
 31                 root = root.left;
 32             }
 33         }
 34         
 35         return total;
 36     }
 37 }
 38 
 39 public class Solution1 {
 40     private int Height(TreeNode node)
 41     {
 42         return node == null ? 0 : 1 + Height(node.left);
 43     }
 44     
 45     public int CountNodes(TreeNode root) {
 46         if (root == null) return 0;
 47         
 48         int leftHeight = Height(root.left), rightHeight = Height(root.right);
 49         
 50         return leftHeight == rightHeight ? (1 << leftHeight) + CountNodes(root.right) : (1 << rightHeight) + CountNodes(root.left);
 51     }
 52 }
 53 
 54 public class Solution2 {
 55     public int CountNodes(TreeNode root) {
 56         if (root == null) return 0;
 57         var lheight = 0;
 58         var n = root;
 59         
 60         while (n != null)
 61         {
 62             lheight++;
 63             n = n.left;
 64         }
 65         
 66         var rheight = 0;
 67         n = root;
 68         
 69         while (n != null)
 70         {
 71             rheight++;
 72             n = n.right;
 73         }
 74         
 75         if (lheight == rheight) return (int)Math.Pow(2, lheight) - 1;
 76         
 77         var count = new int[1] {0};
 78         CountLeafNodes(root, 1, lheight, count);
 79         
 80         return (int)Math.Pow(2, rheight) - 1 + count[0];
 81     }
 82     
 83     private bool CountLeafNodes(TreeNode node, int h, int height, int[] count)
 84     {
 85         if (h == height - 1)
 86         {
 87             if (node.left == null)
 88             {
 89                 return true;
 90             }
 91             else if (node.right == null)
 92             {
 93                 count[0]++;
 94                 return true;
 95             }
 96             
 97             count[0] += 2;
 98             return false;
 99         }
100         
101         return CountLeafNodes(node.left, h + 1, height, count) || CountLeafNodes(node.right, h + 1, height, count);
102     }
103 }
104 
105 public class Solution3 {
106     public int CountNodes(TreeNode root) {
107         if (root == null) return 0;
108         return 1 + CountNodes(root.left) + CountNodes(root.right);
109     }
110 }

 

 
posted @ 2017-12-03 03:10  逸朵  阅读(157)  评论(0)    收藏  举报