java.lang.Object
g1401_1500.s1448_count_good_nodes_in_binary_tree.Solution

public class Solution extends Object
1448 - Count Good Nodes in Binary Tree.<p>Medium</p> <p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named <strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p><strong>Example 1:</strong></p> <p><strong><img src="https://assets.leetcode.com/uploads/2020/04/02/test_sample_1.png" alt="" /></strong></p> <p><strong>Input:</strong> root = [3,1,4,3,null,1,5]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> Nodes in blue are <strong>good</strong>.</p> <p>Root Node (3) is always a good node.</p> <p>Node 4 -> (3,4) is the maximum value in the path starting from the root.</p> <p>Node 5 -> (3,4,5) is the maximum value in the path</p> <p>Node 3 -> (3,1,3) is the maximum value in the path.</p> <p><strong>Example 2:</strong></p> <p><strong><img src="https://assets.leetcode.com/uploads/2020/04/02/test_sample_2.png" alt="" /></strong></p> <p><strong>Input:</strong> root = [3,3,null,4,2]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Node 2 -> (3, 3, 2) is not good, because &ldquo;3&rdquo; is higher than it.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> root = [1]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> Root is considered as <strong>good</strong>.</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the binary tree is in the range <code>[1, 10^5]</code>.</li> <li>Each node&rsquo;s value is between <code>[-10^4, 10^4]</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • goodNodes

      public int goodNodes(TreeNode root)