Created
November 18, 2024 20:59
-
-
Save ghadj/6593abe426b9b012df9254ab779fe522 to your computer and use it in GitHub Desktop.
Leetcode #1448
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* --- | |
* [https://leetcode.com/problems/count-good-nodes-in-binary-tree] | |
* 1448. Count Good Nodes in Binary Tree | |
* Given a binary tree root, a node X in the tree is named good if in the path from root to X | |
* there are no nodes with a value greater than X. | |
* | |
* Return the number of good nodes in the binary tree. | |
* --- | |
*/ | |
class Solution { | |
private int dfs(TreeNode node, int maxValue) { | |
if (node == null) | |
return 0; | |
int count = 0; | |
if (node.val >= maxValue) | |
count = 1; | |
int newMax = Math.max(maxValue, node.val); | |
count += dfs(node.left, newMax); | |
count += dfs(node.right, newMax); | |
return count; | |
} | |
public int goodNodes(TreeNode root) { | |
return dfs(root, root.val); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment