Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • 백트래킹
  • 프로그래머스
  • boj
  • Kubernetes
  • Network
  • docker
  • BFS
  • django
  • 다이나믹 프로그래밍
  • DFS

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/LeetCode

[LeetCode] 1448. Count Good Nodes in Binary Tree (C#)

2024. 5. 17. 21:08

https://leetcode.com/problems/count-good-nodes-in-binary-tree/

 

Root 노드부터 현재 노드(X)까지 이동하는 동안 자신(X)보다 큰 값을 가진 노드가 없는 경우 X는 GoodNode가 된다.

이때 주어진 이진 트리의 GoodNode 개수를 구하는 문제.

 

 

1. 풀이

  • 현재 노드의 값이 지금까지 지나온 값들과 비교해 같거나 크면 GoodNode 카운팅
namespace PS.LeetCode;

public class Solution_1448_1
{
    public int GoodNodes(TreeNode root)
    {
        var result = 0;
        Dfs(root, root.val, ref result);

        return result;
    }

    void Dfs(TreeNode cur, int largestVal, ref int goodCnt)
    {
        if (cur == null)
        {
            return;
        }

        if (cur.val >= largestVal)
        {
            goodCnt++;
        }

        largestVal = Math.Max(largestVal, cur.val);
        
        Dfs(cur.left, largestVal, ref goodCnt);
        Dfs(cur.right, largestVal, ref goodCnt);
    }
}

'Algorithm > LeetCode' 카테고리의 다른 글

[LeetCode] 328. Odd Even Linked List (C#)  (0) 2024.05.17
[LeetCode] 2095. Delete the Middle Node of a Linked List (C#)  (5) 2024.05.12
[LeetCode] 238. Product of Array Except Self (C#)  (0) 2024.05.12
    'Algorithm/LeetCode' 카테고리의 다른 글
    • [LeetCode] 328. Odd Even Linked List (C#)
    • [LeetCode] 2095. Delete the Middle Node of a Linked List (C#)
    • [LeetCode] 238. Product of Array Except Self (C#)

    티스토리툴바