Skip to content

Commit d8b08dc

Browse files
authored
[moonjonghoo]25.02.11 (#41)
* String_Reversal / 기초 * control_z / 기초 * I_don't_like_English /기초 * String_Calculation / 기초 * Crane_Doll_Picking_Game / 중급 * 순서쌍의_개수 / 기초 * 점의_위치_구하기 / 기초 * 로그인_성공? / 기초 * 특이한_정렬 / 기초 * 프로세스 / 고급 * 프로세스 /고급 수정 * 모스부호(1) / 기초 * A로 B만들기 /기초 * 진료순서/ 기초 * 등수매기기 /기초 * 완주하지 못한 선수 /중급 * maximum_depth_of_binary_tree / 기초 * binary_tree_inorder_traversal /기초 * same_tree /기초 * Invert_Binary_tree/기초
1 parent e94099c commit d8b08dc

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var invertTree = function (root) {
2+
if (root === null) {
3+
return null;
4+
}
5+
6+
// 왼쪽과 오른쪽 자식 노드를 교환
7+
const temp = root.left;
8+
root.left = root.right;
9+
root.right = temp;
10+
11+
// 재귀적으로 자식 노드들을 반전
12+
invertTree(root.left);
13+
invertTree(root.right);
14+
15+
return root;
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var inorderTraversal = function (root) {
2+
const result = [];
3+
4+
const traverse = (node) => {
5+
if (node === null) return;
6+
traverse(node.left); // 왼쪽 서브트리 방문
7+
result.push(node.val); // 현재 노드 방문
8+
traverse(node.right); // 오른쪽 서브트리 방문
9+
};
10+
11+
traverse(root);
12+
return result;
13+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 {number}
12+
*/
13+
var maxDepth = function (root) {
14+
if (!root) return 0;
15+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
16+
};
17+
18+
console.log(maxDepth);

Moonjonghoo/tree/same_tree.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var isSameTree = function (p, q) {
2+
// 두 노드가 모두 null인 경우
3+
if (p === null && q === null) {
4+
return true;
5+
}
6+
// 한 노드만 null인 경우
7+
if (p === null || q === null) {
8+
return false;
9+
}
10+
// 노드의 값이 다른 경우
11+
if (p.val !== q.val) {
12+
return false;
13+
}
14+
// 왼쪽 및 오른쪽 서브트리를 재귀적으로 비교
15+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
16+
};

0 commit comments

Comments
 (0)