1448. Count Good Nodes in Binary Tree
Solution
Recursive calculate the node with DFS
int count = 0;
public int goodNodes(TreeNode root) {
dfs(root, -10000);
return count;
}
public void dfs(TreeNode node, int max){
if(node == null) return;
if(node.val >= max){
max = node.val;
count++;
}
dfs(node.left, max);
dfs(node.right, max);
}
Last updated
Was this helpful?