Skip to content

Commit 58982e5

Browse files
[oceanlee-seoul] 24.11.19 (#103)
* 102 / Binary Tree Level Order Traversal / Medium / 60m * 572 / Subtree of Another Tree / Easy / 30m
1 parent d92c002 commit 58982e5

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 isBalanced = function (root) {
14+
const checkHeight = (node) => {
15+
if (node === null) return 0;
16+
17+
const leftHeight = checkHeight(node.left);
18+
const rightHeight = checkHeight(node.right);
19+
20+
if (
21+
leftHeight === -1 ||
22+
rightHeight === -1 ||
23+
Math.abs(leftHeight - rightHeight) > 1
24+
) {
25+
return -1;
26+
}
27+
28+
return Math.max(leftHeight, rightHeight) + 1;
29+
};
30+
31+
return checkHeight(root) !== -1;
32+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var singleNumber = function (nums) {
6+
if (nums.length === 1) return nums[0];
7+
8+
const mapObj = {};
9+
for (num of nums) {
10+
if (!mapObj[num]) {
11+
mapObj[num] = 1;
12+
} else {
13+
mapObj[num] += 1;
14+
}
15+
}
16+
return +Object.keys(mapObj).find((key) => mapObj[key] === 1);
17+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 levelOrder = function (root) {
14+
if (!root) return [];
15+
16+
const result = [];
17+
const queue = [root];
18+
19+
while (queue.length > 0) {
20+
const levelSize = queue.length;
21+
const levelList = [];
22+
23+
for (let i = 0; i < levelSize; i++) {
24+
const node = queue.shift();
25+
26+
levelList.push(node.val);
27+
28+
if (node.left) queue.push(node.left);
29+
30+
if (node.right) queue.push(node.right);
31+
}
32+
result.push(levelList);
33+
}
34+
return result;
35+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
* @param {TreeNode} subRoot
12+
* @return {boolean}
13+
*/
14+
var isSubtree = function (root, subRoot) {
15+
if (!root) return false;
16+
17+
if (isSameTree(root, subRoot)) return true;
18+
19+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
20+
};
21+
22+
var isSameTree = function (t1, t2) {
23+
if (!t1 && !t2) return true;
24+
if (!t1 || !t2) return false;
25+
if (t1.val !== t2.val) return false;
26+
27+
return isSameTree(t1.left, t2.left) && isSameTree(t1.right, t2.right);
28+
};

0 commit comments

Comments
 (0)