File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
src/validate-binary-search-tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
5252| 88| [ Merge Sorted Array] ( https://leetcode.com/problems/merge-sorted-array/ ) | [ JavaScript] ( ./src/merge-sorted-array/res.js ) | Medium|
5353| 91| [ Decode Ways] ( https://leetcode.com/problems/decode-ways/ ) | [ JavaScript] ( ./src/decode-ways/res.js ) | Medium|
5454| 93| [ Restore IP Addresses] ( https://leetcode.com/problems/restore-ip-addresses/ ) | [ JavaScript] ( ./src/restore-ip-addresses/res.js ) | Medium|
55+ | 98| [ Validate Binary Search Tree] ( https://leetcode.com/problems/validate-binary-search-tree/ ) | [ JavaScript] ( ./src/validate-binary-search-tree/res.js ) | Medium|
5556| 100| [ Same Tree] ( https://leetcode.com/problems/same-tree/ ) | [ JavaScript] ( ./src/same-tree/res.js ) | Easy|
5657| 101| [ Symmetric Tree] ( https://leetcode.com/problems/symmetric-tree/ ) | [ JavaScript] ( ./src/symmetric-tree/res.js ) | Easy|
5758| 102| [ Binary Tree Level Order Traversal] ( https://leetcode.com/problems/binary-tree-level-order-traversal/ ) | [ JavaScript] ( ./src/binary-tree-level-order-traversal/res.js ) | Medium|
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val) {
4+ * this.val = val;
5+ * this.left = this.right = null;
6+ * }
7+ */
8+ /**
9+ * @param {TreeNode } root
10+ * @return {boolean }
11+ */
12+ var isValidBST = function ( root ) {
13+ if ( ! root ) return true ;
14+
15+ const validSubBST = ( element , low , high ) => {
16+ if ( low !== null && low >= element . val ) return false ;
17+ if ( high !== null && high <= element . val ) return false ;
18+ if ( ! element . left && ! element . right ) return true ;
19+
20+ if ( element . left && ! validSubBST ( element . left , low , element . val ) ) return false ;
21+ if ( element . right && ! validSubBST ( element . right , element . val , high ) ) return false ;
22+
23+ return true ;
24+ }
25+
26+ return validSubBST ( root , null , null ) ;
27+ } ;
You can’t perform that action at this time.
0 commit comments