码迷,mamicode.com
首页 > 其他好文 > 详细

250.Count Univalue Subtrees

时间:2016-06-20 08:53:13      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     *250.Count Univalue Subtrees
     *2016-6-18 by Mingyang 
     *这个题目出的很好,google的电话面试的水平
     *求Uni-value subtree的个数。对树来说这求count的首先思路就是递归了,
     *不过这里要另外构造一个辅助函数来判断root为顶点的subtree是否是Uni-value subtree,
     *假如是Uni-value的subtree,则结果可以+1,否则,我们返回递归求出的左子树的count和右节点的count。
     *Time Complexity - O(n), Space Complexity - O(n).
     *Given a binary tree, count the number of uni-value subtrees.
     *A Uni-value subtree means all nodes of the subtree have the same value.
     *For example:  return 4
     *Given binary tree,

              5
             /             1   5
           / \             5   5   5
     */
    //我的基本代码,不够优化
       public int countUnivalSubtrees(TreeNode root) {
            if(root == null)
                return 0;
            if(root.left == null && root.right == null)
                return 1;
            if(root.left == null) {
                if(isUniValueSubtree(root))
                    return countUnivalSubtrees(root.right) + 1;
                else
                    return countUnivalSubtrees(root.right);
            } else if (root.right == null) {
                if(isUniValueSubtree(root))
                    return countUnivalSubtrees(root.left) + 1;
                else
                    return countUnivalSubtrees(root.left);
            } else {
                if(isUniValueSubtree(root))
                    return countUnivalSubtrees(root.left) + countUnivalSubtrees(root.right) + 1;
                else
                    return countUnivalSubtrees(root.left) + countUnivalSubtrees(root.right);
            }
        }    
        private boolean isUniValueSubtree(TreeNode root) {
            if(root == null)
                return true;
            if(root.left == null && root.right == null)
                return true;
            else if (root.left != null && root.right != null) {
                if(root.val == root.left.val && root.val == root.right.val)
                    return isUniValueSubtree(root.left) && isUniValueSubtree(root.right);
                else
                    return false;
            } else if (root.left == null) {
                if(root.right.val == root.val)
                    return isUniValueSubtree(root.right);
                else
                    return false;
            } else {
                if(root.left.val == root.val)
                    return isUniValueSubtree(root.left);
                else
                    return false;
            }
        }
        /*
         * 优化版本!!!!!!!!!!!
         * 只有两种情况才加加,一种就是我们遇到叶子节点的时候,另一种就是我们遇到三个都相等的节点的时候
         */
        private int count = 0;
        public int countUnivalSubtrees1(TreeNode root) {
            unival(root);
            return count;
        }
        private boolean unival(TreeNode root) {
            if (root == null)
                return true;
            if (root.left == null && root.right == null) {
                count++;
                return true;
            }
            boolean left = unival(root.left);//自底向上第一步,走到最底的那一层
            boolean right = unival(root.right);
            if (left && right && (root.left == null || root.left.val == root.val)&& (root.right == null || root.right.val == root.val)) {
                count++;
                return true;
            }
            return false;
        }

 

250.Count Univalue Subtrees

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5599466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!