Talentd
FleetCode

3997. Count Dominant Nodes in a Binary Tree

Medium72.1% acceptance

You are given the root of a complete binary tree.

A node x is called dominant if its value is equal to the maximum value among all nodes in the subtree rooted at x.

Return the number of dominant nodes in the tree.

Example 1:

Input: root = [5,3,8,2,4,7,1]

Output: 5

Explanation:

  • The leaf nodes with values 2, 4, 7, and 1 are dominant.
  • The node with value 8 is dominant because its value is the maximum value in its subtree [8, 7, 1].
  • Thus, the answer is 5.

Example 2:

Input: root = [1,2,3,1,2]

Output: 4

Explanation:

  • The leaf nodes with values 1, 2, and 3 are dominant.
  • The node with value 2 whose subtree is [2, 1, 2] is dominant because its value is the maximum value in its subtree.
  • Thus, the answer is 4.

Constraints:

  • The number of nodes in the tree is in the range [1, 105].
  • 1 <= Node.val <= 109
  • The tree is guaranteed to be a complete binary tree.

Hints open one at a time

Hint 1

Process the tree using postorder traversal, so both child subtrees are handled before their parent.

Hint 2

For each node, compute the maximum value in its subtree from its own value and the maximum values returned by its children.

Hint 3

A node is dominant if its value is equal to this subtree maximum.

5:23
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for a binary tree node.
2# class TreeNode:
3# def __init__(self, val=0, left=None, right=None):
4# self.val = val
5# self.left = left
6# self.right = right
7class Solution:
8 def countDominantNodes(self, root: TreeNode | None) -> int:
9
Starter codePython 3Spaces: 4
Examples
Input
[5,3,8,2,4,7,1]
Input
[1,2,3,1,2]
Your code is saved on this device.Solution