LeetCode - Medium - 1448. Count Good Nodes in Binary Tree

超、凢脫俗 2022-10-07 13:55 95阅读 0赞

Topic

  • Tree
  • Depth-first Search

Description

https://leetcode.com/problems/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.

Example 1:

353481f2e6499ab0d25d68faf695dff4.png

  1. Input: root = [3,1,4,3,null,1,5]
  2. Output: 4
  3. Explanation: Nodes in blue are good.
  4. Root Node (3) is always a good node.
  5. Node 4 -> (3,4) is the maximum value in the path starting from the root.
  6. Node 5 -> (3,4,5) is the maximum value in the path
  7. Node 3 -> (3,1,3) is the maximum value in the path.

Example 2:

6a961acb6d9820da72043a0cbeb4e634.png

  1. Input: root = [3,3,null,4,2]
  2. Output: 3
  3. Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.

Example 3:

  1. Input: root = [1]
  2. Output: 1
  3. Explanation: Root is considered as good.

Constraints:

  • The number of nodes in the binary tree is in the range [ 1 , 1 0 5 ] [1, 10^5] [1,105].
  • Each node’s value is between [ − 1 0 4 , 1 0 4 ] [-10^4, 10^4] [−104,104].

Analysis

方法一:递归版DFS

方法二:迭代版DFS

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class CountGoodNodesInBinaryTree {
  4. //方法一:递归版
  5. public int goodNodes(TreeNode root) {
  6. return goodNodes(root, Integer.MIN_VALUE);
  7. }
  8. public int goodNodes(TreeNode node, int max) {
  9. if(node == null) return 0;
  10. int nextMax = Math.max(max, node.val);
  11. return (node.val >= max ? 1 : 0) + goodNodes(node.left, nextMax)//
  12. + goodNodes(node.right, nextMax);
  13. }
  14. //方法二:迭代版
  15. public int goodNodes2(TreeNode root) {
  16. if(root == null)
  17. return 0;
  18. int result = 0;
  19. LinkedList<Object[]> stack = new LinkedList<>();
  20. stack.add(new Object[] { root, Integer.MIN_VALUE});
  21. while(!stack.isEmpty()) {
  22. Object[] arr = stack.pop();
  23. TreeNode node = (TreeNode)arr[0];
  24. int max = (int)arr[1];
  25. if(node.val >= max)
  26. result++;
  27. int nextMax = Math.max(max, node.val);
  28. if(node.right != null)
  29. stack.push(new Object[] { node.right, nextMax});
  30. if(node.left != null)
  31. stack.push(new Object[] { node.left, nextMax});
  32. }
  33. return result;
  34. }
  35. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. public class CountGoodNodesInBinaryTreeTest {
  5. @Test
  6. public void test() {
  7. CountGoodNodesInBinaryTree obj = new CountGoodNodesInBinaryTree();
  8. assertEquals(4, obj.goodNodes(BinaryTree.integers2BinaryTree(3, 1, 4, 3, null, 1, 5)));
  9. assertEquals(3, obj.goodNodes(BinaryTree.integers2BinaryTree(3, 3, null, 4, 2)));
  10. assertEquals(1, obj.goodNodes(BinaryTree.integers2BinaryTree(1)));
  11. }
  12. @Test
  13. public void test2() {
  14. CountGoodNodesInBinaryTree obj = new CountGoodNodesInBinaryTree();
  15. assertEquals(4, obj.goodNodes2(BinaryTree.integers2BinaryTree(3, 1, 4, 3, null, 1, 5)));
  16. assertEquals(3, obj.goodNodes2(BinaryTree.integers2BinaryTree(3, 3, null, 4, 2)));
  17. assertEquals(1, obj.goodNodes2(BinaryTree.integers2BinaryTree(1)));
  18. }
  19. }

发表评论

表情:
评论列表 (有 0 条评论,95人围观)

还没有评论,来说两句吧...

相关阅读