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 |
Tags
- 파이썬프로그래밍기초
- node.js
- 꿀단집
- redis
- presignedurl
- 파이썬
- Python
- 중간이들
- aws
- 방송대
- nestjs
- 항해99
- 코드잇
- 엘리스sw트랙
- 방송대컴퓨터과학과
- SQL
- 데이터베이스시스템
- 99클럽
- 유노코딩
- Cookie
- 프로그래머스
- 코딩테스트준비
- TiL
- 개발자취업
- JavaScript
- Git
- MySQL
- 코딩테스트
- CSS
- HTML
Archives
- Today
- Total
배꼽파지 않도록 잘 개발해요
99클럽 코테 스터디 18일차 TIL - Increasing Order Search Tree 본문
// Interface for a binary tree node.
interface TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
}
// Function to create a new TreeNode with a specific value.
function createTreeNode(val: number, left: TreeNode | null = null, right: TreeNode | null = null): TreeNode {
return {
val: val,
left: left,
right: right
};
}
let previousNode: TreeNode | null; // Variable to keep track of the previously processed node.
// Function that takes a binary search tree and rearranges it so that it becomes a strictly
// increasing order tree where each node only has a right child following an in-order traversal
// of the tree.
function increasingBST(root: TreeNode): TreeNode {
const dummyNode: TreeNode = createTreeNode(0); // Create a dummy node that will act as the "previous node" of the first node in the in-order traversal.
previousNode = dummyNode; // Initialize previousNode with the dummy node.
// Start the in-order traversal and rearrange the tree.
inOrderTraversal(root);
// Return the right child of the dummy node, which is the new root of the rearranged tree.
return dummyNode.right!;
}
// Helper function that performs a depth-first search (DFS) in-order traversal of the binary tree.
function inOrderTraversal(currentNode: TreeNode | null): void {
if (currentNode === null) return; // If the current node is null, do nothing and return.
// First, traverse the left subtree (in-order).
inOrderTraversal(currentNode.left);
// Assign the current node to the right child of the previous node.
if (previousNode) previousNode.right = currentNode;
// The current node should not have a left child in the rearranged tree.
currentNode.left = null;
// Update the previousNode to be the current node after processing it.
previousNode = currentNode;
// Next, traverse the right subtree (in-order).
inOrderTraversal(currentNode.right);
}
728x90
'코딩테스트 > 99클럽' 카테고리의 다른 글
99클럽 코테 스터디 20일차 TIL - 체육복 (0) | 2024.08.11 |
---|---|
99클럽 코테 스터디 19일차 TIL - 과일 장 (0) | 2024.08.09 |
99클럽 코테 스터디 17일차 TIL - Binary Tree Inorder Traversal (0) | 2024.08.07 |
99클럽 코테 스터디 16일차 TIL - 최소 직사각형 (0) | 2024.08.06 |
99클럽 코테 스터디 15일차 TIL - 모의고사 (0) | 2024.08.05 |