250.Count Univalue Subtrees

Posted 我的名字叫周周

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了250.Count Univalue Subtrees相关的知识,希望对你有一定的参考价值。

    /*
     *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的主要内容,如果未能解决你的问题,请参考以下文章

250. Count Univalue Subtrees

250.Count Univalue Subtrees

[LC] 250. Count Univalue Subtrees

[LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数

LeetCode Count Univalue Subtrees

687. Longest Univalue Path