File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
validate-binary-search-tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * @param {TreeNode } root
11+ * @return {boolean }
12+ */
13+ var isValidBST = function ( root ) {
14+ function helper ( node , lower = - Infinity , upper = Infinity ) {
15+ if ( ! node ) return true ;
16+
17+ const val = node . val ;
18+
19+ // ํ์ฌ ๋
ธ๋๊ฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด false
20+ if ( val <= lower || val >= upper ) {
21+ return false ;
22+ }
23+
24+ // ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ: ์ต์๊ฐ์ ํ์ฌ ๋
ธ๋ ๊ฐ
25+ if ( ! helper ( node . right , val , upper ) ) {
26+ return false ;
27+ }
28+
29+ // ์ผ์ชฝ ์๋ธํธ๋ฆฌ: ์ต๋๊ฐ์ ํ์ฌ ๋
ธ๋ ๊ฐ
30+ if ( ! helper ( node . left , lower , val ) ) {
31+ return false ;
32+ }
33+
34+ return true ;
35+ }
36+
37+ return helper ( root ) ;
38+ } ;
You canโt perform that action at this time.
0 commit comments