Algo-Sim › Binary Trees & BST
Count Good Nodes in Binary Tree
Medium · Binary Trees & BST · step through it in Java, Python and C++ on your own input
A node is good if no value on the path from the root to it is larger. Carry the path maximum down a DFS.
Problem
Given the root of a binary tree, a node X is good if no node on the path from the root to X has a value greater than X. Return the number of good nodes. The root is always good.
Constraints
- 1 ≤ number of nodes ≤ 10⁵
- -10⁴ ≤ Node.val ≤ 10⁴
- Equal values do not spoil a node: X ≥ every value above it
Edge cases to test
- One node:
1 - Equal values count as good
- A strictly decreasing chain
Follow-up questions
- Why pass the maximum down?
- Each node only needs the largest value above it, so one O(n) DFS with O(h) stack is enough.
- Iterative version?
- A stack of (node, max so far) pairs.
Solution
Java
public int goodNodes(TreeNode root) {
return count(root, Integer.MIN_VALUE);
}
private int count(TreeNode node, int maxSoFar) {
if (node == null) return 0;
int good = node.val >= maxSoFar ? 1 : 0;
int best = Math.max(maxSoFar, node.val);
int left = count(node.left, best);
int right = count(node.right, best);
return good + left + right;
}Python
def good_nodes(root):
def count(node, max_so_far):
if not node:
return 0
good = 1 if node.val >= max_so_far else 0
best = max(max_so_far, node.val)
left = count(node.left, best)
right = count(node.right, best)
return good + left + right
return count(root, float("-inf"))C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int goodNodes(TreeNode *root) {
return count(root, INT_MIN);
}
int count(TreeNode *node, int maxSoFar) {
if (node == nullptr)
return 0;
int good = node->val >= maxSoFar ? 1 : 0;
int best = std::max(maxSoFar, node->val);
int left = count(node->left, best);
int right = count(node->right, best);
return good + left + right;
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)