File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
validate-binary-search-tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # https://leetcode.com/problems/validate-binary-search-tree/description/
3+ # Intuition
4+ BST๋ฅผ ์ค์ ์ํํ๋ฉด ์ค๋ฆ์ฐจ์์ผ๋ก ๊ฐ์ด ์ ๋ ฌ๋๋ค๋ ์ ์ ์ฐฉ์ํ์ต๋๋ค.
5+
6+ # Complexity
7+ - Time complexity: ๋
ธ๋์ ๊ฐ์๋ฅผ N์ด๋ผ๊ณ ํ ๋, O(N)
8+
9+ - Space complexity: ์ฌ๊ท๋ก ํธ๋ฆฌ๋ฅผ ์ํํ๋ฉด์ ํธ์ถ ์คํ์ด ์์ด๋๋ฐ
10+ ํธ๋ฆฌ์ ๋์ด๋ฅผ H๋ผ๊ณ ํ ๋, ํธ์ถ ์คํ์ H ๋งํผ ์์
๋๋ค. => ๊ณต๊ฐ ๋ณต์ก๋ O(H)
11+ """
12+
13+
14+ class Solution :
15+ def isValidBST (self , root : Optional [TreeNode ]) -> bool :
16+ prev = [None ]
17+
18+ def inorder (node ):
19+ if not node :
20+ return True
21+
22+ if not inorder (node .left ):
23+ return False
24+
25+ if prev [0 ] is not None and prev [0 ] >= node .val :
26+ return False
27+ prev [0 ] = node .val
28+
29+ return inorder (node .right )
30+
31+ return inorder (root )
You canโt perform that action at this time.
0 commit comments