Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 컴퓨터과학
- multer-s3
- lightsailor
- Search Algorithm
- 문자열
- 배포
- node.js
- Local Search
- typescript
- 알고리즘
- 파이썬
- Hill Climbing
- computerscience
- 코드
- 컴퓨터공학
- CS
- 문자열처리
- Simulated Annealing
- 철학
- AWS
- node배포
Archives
- Today
- Total
지식의모듈화
[LeetCode][Javascript] Binary Search Tree Problems 본문
98. Validate Binary Search Tree
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
// console.log(root.left, root.right, root, "VALID");
let arr=[];
let ans=true;
function DFS(current){
if(current==null)return;
DFS(current.left);
// console.log(current.val);
arr.push(current.val);
DFS(current.right);
}
DFS(root)
// console.log(arr);
for(let i=0; i<arr.length-1; i++){
if(arr[i]>= arr[i+1]) ans=false;
}
return ans;
};
O(N) solution, time complexity 개선 가능하다.
104. Maximum Depth of Binary Tree
var maxDepth = function(root) {
if(!root) return 0;
return Math.max(maxDepth(root.right)+1, maxDepth(root.left)+1)
};
230. Kth Smallest Element in a BST
var kthSmallest = function(root, k) {
let arr=[];
function DFS(current){
if (current==null) return;
DFS(current.left)
// console.log(current.val);
arr.push(current.val)
DFS(current.right)
}
DFS(root);
// console.log(arr);
return arr[k-1];
};
O(N)으로, time complexity개선 가능하다.
'PS > LeetCode' 카테고리의 다른 글
[Leetcode][Javascript] 36. Valid Sudoku (0) | 2022.08.17 |
---|---|
[LeetCode][Javascript] LinkedList Problems (0) | 2022.08.05 |
[LeetCode][JavaScript] DFS Problems (0) | 2022.08.04 |
[CoderPad Interview] Problem 77 [Easy] (0) | 2022.08.04 |
[LeetCode][JavaScript] 1. TwoSum (0) | 2022.08.04 |