Skip to content

Commit d92c002

Browse files
authored
[heony704] 24.11.19 (#102)
* 102 / Binary Tree Level Order Traversal / Medium / 36m * 150 / Evaluate Reverse Polish Notation / Medium / 40m * 572 / Subtree of Another Tree / Easy / 39m
1 parent 5931cd7 commit d92c002

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/binary-tree-level-order-traversal/
3+
* 36m
4+
*/
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* function TreeNode(val, left, right) {
9+
* this.val = (val===undefined ? 0 : val)
10+
* this.left = (left===undefined ? null : left)
11+
* this.right = (right===undefined ? null : right)
12+
* }
13+
*/
14+
/**
15+
* @param {TreeNode} root
16+
* @return {number[][]}
17+
*/
18+
var levelOrder = function (root) {
19+
const levelOrders = [];
20+
let targetNodes = [root];
21+
22+
while (root) {
23+
const nextTargets = [];
24+
const currentLevels = [];
25+
for (const targetNode of targetNodes) {
26+
currentLevels.push(targetNode.val);
27+
28+
if (targetNode.left !== null) nextTargets.push(targetNode.left);
29+
if (targetNode.right !== null) nextTargets.push(targetNode.right);
30+
}
31+
levelOrders.push(currentLevels);
32+
if (nextTargets.length < 1) break;
33+
targetNodes = nextTargets;
34+
}
35+
36+
return levelOrders;
37+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* https://leetcode.com/problems/evaluate-reverse-polish-notation/
3+
* 40m
4+
*/
5+
6+
/**
7+
* @param {string[]} tokens
8+
* @return {number}
9+
*/
10+
var evalRPN = function (tokens) {
11+
const numStack = [];
12+
13+
for (const token of tokens) {
14+
if (Number.isInteger(Number(token))) {
15+
numStack.push(Number(token));
16+
} else {
17+
const second = numStack.pop();
18+
const first = numStack.pop();
19+
20+
if (token === "+") numStack.push(first + second);
21+
if (token === "-") numStack.push(first - second);
22+
if (token === "*") numStack.push(first * second);
23+
if (token === "/") numStack.push(Math.trunc(first / second));
24+
}
25+
}
26+
27+
return numStack[0];
28+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/subtree-of-another-tree/
3+
* 39m
4+
*/
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* function TreeNode(val, left, right) {
9+
* this.val = (val===undefined ? 0 : val)
10+
* this.left = (left===undefined ? null : left)
11+
* this.right = (right===undefined ? null : right)
12+
* }
13+
*/
14+
/**
15+
* @param {TreeNode} root
16+
* @param {TreeNode} subRoot
17+
* @return {boolean}
18+
*/
19+
20+
var isSameTree = function (one, theOther) {
21+
if (!one && !theOther) return true;
22+
else if (!one || !theOther) return false;
23+
if (one.val !== theOther.val) return false;
24+
if (!isSameTree(one.left, theOther.left)) return false;
25+
if (!isSameTree(one.right, theOther.right)) return false;
26+
27+
return true;
28+
};
29+
30+
var isSubtree = function (root, subRoot) {
31+
if (isSameTree(root, subRoot)) return true;
32+
33+
if (root.left && isSubtree(root.left, subRoot)) return true;
34+
if (root.right && isSubtree(root.right, subRoot)) return true;
35+
36+
return false;
37+
};

0 commit comments

Comments
 (0)