[LeetCode#783] Minimum Distance Between BST Nodes

2024. 9. 25. 22:19·Algorithm/문제풀이

❒ Description


날짜 2024.09.25 (수)
레벨 Easy
제목 Minimum Distance Between BST Nodes
링크 https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/
자료구조 그래프 - BST
시간 복잡도 O(n)

 

[LeetCode#1038] Binary Search Tree to Greater Sum Tree 문제와 거의 유사한 문제이다.

 

 

 

 

 

❒ Solution


private int prev = Integer.MIN_VALUE + 10000;
private int minDiff = Integer.MAX_VALUE;

public int minDiffInBST(TreeNode root) {
    if (root != null) {
        minDiffInBST(root.left);
        minDiff = Math.min(minDiff, root.val - prev);
        prev = root.val;
        minDiffInBST(root.right);
    }
    return minDiff;
}

 

 

 

 

 


'Algorithm > 문제풀이' 카테고리의 다른 글

[LeetCode#148] Sort List  (0) 2024.09.27
[LeetCode#105]Construct BT from Preorder and Inorder Traversal  (0) 2024.09.26
[LeetCode#938] Range Sum of BST  (0) 2024.09.25
[LeetCode#1038] Binary Search Tree to Greater Sum Tree  (0) 2024.09.25
[LeetCode#108] Convert Sorted Array to Binary Search Tree  (0) 2024.09.25
'Algorithm/문제풀이' 카테고리의 다른 글
  • [LeetCode#148] Sort List
  • [LeetCode#105]Construct BT from Preorder and Inorder Traversal
  • [LeetCode#938] Range Sum of BST
  • [LeetCode#1038] Binary Search Tree to Greater Sum Tree
gilbert9172
gilbert9172
gilbert9172 님의 블로그 입니다.
  • gilbert9172
    バックエンド
    gilbert9172
  • 전체
    오늘
    어제
    • All Categories (175)
      • 우테코 7기 (21)
        • 1주차 (8)
        • 2주차 (5)
        • 3주차 (6)
      • Langauge (4)
        • Java (3)
        • Kotlin (1)
      • Back-End (13)
        • SpringBoot (1)
        • Trouble Shooting (0)
        • Setup & Configuration (1)
        • SQL (3)
        • Redis (8)
      • Architecture (6)
        • Multi Module (1)
        • DDD (5)
      • CS (30)
        • Data Structure (6)
        • Operating System (0)
        • Network (12)
        • Database (10)
        • Design Pattern (2)
      • Algorithm (78)
        • 내용 정리 (18)
        • 문제풀이 (60)
      • DevOps (6)
        • AWS (5)
        • Git (1)
      • Front-End (1)
        • Trouble Shooting (1)
      • Project (6)
        • 페이스콕 (6)
      • Book (9)
        • 이벤트 기반 마이크로서비스 구축 (7)
        • 친절한 SQL 튜닝 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Back-Tracking
    sliding-window
    오블완
    greedy
    Two-Pointer
    binarysearch
    부분단조성
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
gilbert9172
[LeetCode#783] Minimum Distance Between BST Nodes
상단으로

티스토리툴바