zoukankan      html  css  js  c++  java
  • [LeetCode] 250. Count Univalue Subtrees

    Given the root of a binary tree, return the number of uni-value subtrees.

    A uni-value subtree means all nodes of the subtree have the same value.

    Example 1:

    Input: root = [5,1,5,5,5,null,5]
    Output: 4
    

    Example 2:

    Input: root = []
    Output: 0
    

    Example 3:

    Input: root = [5,5,5,5,5,null,5]
    Output: 6

    Constraints:

    • The numbrt of the node in the tree will be in the range [0, 1000].
    • -1000 <= Node.val <= 1000

    统计同值子树。

    题意是给一个二叉树,请返回同值子树的个数。同值子树的定义是所有子树的节点值都相同。影子题687

    这个例子里面有4个同值子树,是哪四个呢?是三个叶子节点 + 右子树的两个5。因为他们都满足自身和他们的左右孩子(如存在)的值相同。

    这个题思路是后序遍历(postorder),因为需要先知道叶子节点的值,才能判断当前节点的值是否和他们的孩子相同。这道题想到用后序遍历做不难,难点在于对于一个孩子节点,到底往他的父节点返回什么信息才有用。这道题比较特殊的地方是我们往父节点传的是一个boolean值,表示的是叶子节点是否存在以及叶子节点和父节点的值是否相同。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     int count;
     3 
     4     public int countUnivalSubtrees(TreeNode root) {
     5         count = 0;
     6         helper(root);
     7         return count;
     8     }
     9 
    10     private boolean helper(TreeNode root) {
    11         if (root == null) {
    12             return true;
    13         }
    14         boolean left = helper(root.left);
    15         boolean right = helper(root.right);
    16         if (left && right) {
    17             if (root.left != null && root.val != root.left.val) {
    18                 return false;
    19             }
    20             if (root.right != null && root.val != root.right.val) {
    21                 return false;
    22             }
    23             count++;
    24             return true;
    25         }
    26         return false;
    27     }
    28 }

    JavaScript实现

     1 /**
     2  * @param {TreeNode} root
     3  * @return {number}
     4  */
     5 var countUnivalSubtrees = function (root) {
     6     let count = 0;
     7 
     8     var helper = function (root) {
     9         if (root == null) {
    10             return true;
    11         }
    12         let left = helper(root.left);
    13         let right = helper(root.right);
    14         if (left && right) {
    15             if (root.left != null && root.val != root.left.val) {
    16                 return false;
    17             }
    18             if (root.right != null && root.val != root.right.val) {
    19                 return false;
    20             }
    21             count++;
    22             return true;
    23         }
    24         return false;
    25     }
    26     helper(root);
    27     return count;
    28 };

    相关题目

    250. Count Univalue Subtrees

    687. Longest Univalue Path

    LeetCode 题目总结

  • 相关阅读:
    VMware下安装Ubuntu虚拟机
    py3+urllib+bs4+反爬,20+行代码教你爬取豆瓣妹子图
    老铁,这年头得玩玩这个:Git基本操作【github】
    本地Git与GitHub服务器建立连接(SSH方式通信)
    python开启httpserver服务在自动化测试中的一个小运用
    python测试webservice接口
    Xcache3.2.0不支持php7.0.11
    Nginx设置alias别名目录访问phpmyadmin
    CentOS 7.2.1511编译安装Nginx1.10.1+MySQL5.7.15+PHP7.0.11
    CentOS平滑更新nginx版本
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12484102.html
Copyright © 2011-2022 走看看