[Locked] Count Univalue Subtrees

Count Univalue Subtrees

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:
Given binary tree,

              5
             /             1   5
           / \             5   5   5

return 4.

分析:

  有点像自低向上的动态规划,既然是自底向上,看来用递归访问树的节点无疑可以解决问题

代码:

bool dfs(TreeNode *node, int &count) {
    if(!node)
        return true;
    //一定要让保证其先递归,达到最底层节点,然后作后续处理
    bool goodleft = dfs(node->left, count), goodright = dfs(node->right, count);
    //与左右节点值相同,且左右子树是值相同的树,则以该节点为根节点的树也是值相同的树
    if(goodleft && goodright && (!node->left || node->val == node->left->val) && (!node->right || node->val == node->right->val)) {
        count++;
        return true;
    }
    return false;
}
int count(TreeNode *node) {
    int num = 0;
    dfs(node, num);
    return num;
}
时间: 2024-08-11 03:33:02

[Locked] Count Univalue Subtrees的相关文章

LeetCode Count Univalue Subtrees

原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ 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:Given binary tree, 5 / 1 5 / \ 5 5 5 retur

250. Count Univalue Subtrees

题目: 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:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 链接: http://leetcode.com/problems/count-univalue-subtre

[LeetCode#250] Count Univalue Subtrees

Problem: 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:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. Analysis: This problem is super simple. But, beca

[LeetCode] Count Univalue Subtrees 计数相同值子树的个数

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:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest BST

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

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:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 给一个二叉树,求唯一值子树的个数.唯一值子树的所有节点具有相同值. 解法:递归 Java: /** * Defini

[LC] 250. Count Univalue Subtrees

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. Example : Input: root = [5,1,5,5,5,null,5] 5 / 1 5 / \ 5 5 5 Output: 4 /** * Definition for a binary tree node. * pub

[LeetCode] 687. Longest Univalue Path 最长唯一值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.