/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public static int cnt;
public int goodNodes(TreeNode root) {
cnt = 0;
count(root, root.val);
return cnt;
}
public void count(TreeNode node, int parent) {
if (node == null) {
return;
}
if (node.val >= parent) {
cnt += 1;
parent = node.val;
}
count(node.left, parent);
count(node.right, parent);
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 994. Rotting Oranges (0) | 2024.06.14 |
---|---|
[leetcode] 1161. Maximum Level Sum of a Binary Tree (0) | 2024.06.14 |
[leetcode] 872. Left-Similar Leafs (0) | 2024.06.11 |
[leetcode] 443. String Compression (0) | 2024.06.11 |
[leetcode] 724. Find Pivot Index (1) | 2024.06.09 |