Skip to content

Commit 521ec62

Browse files
BarklimBarklim
authored andcommitted
add tree examples
1 parent 47d6447 commit 521ec62

File tree

20 files changed

+453
-13
lines changed

20 files changed

+453
-13
lines changed

example/5.Stack/2390.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
* @return {string}
44
*/
55
var removeStars = function(s) {
6-
const stack = [];
76

8-
for (const char of s) {
9-
char === '*' ? stack.pop(): stack.push(char)
10-
}
11-
12-
return stack.join('');
137
};
148

159
const example1 = removeStars('leet**cod*e'); // 'lecoe'

example/6.Tree/0098.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 isValidBST = function(root) {
14+
15+
};
16+
17+
const example1 = isValidBST(); // root = [2,1,3] // true
18+
const example2 = isValidBST(); // root = [5,1,4,null,null,3,6] // false
19+
const example3 = isValidBST(); // root = [1,2,3] // false
20+
21+
console.log(example1);
22+
console.log(example2);
23+
console.log(example3);

example/6.Tree/0100.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function(p, q) {
15+
16+
};
17+
18+
const example1 = isSameTree(); // p = [1,2,3], q = [1,2,3] // true
19+
const example2 = isSameTree(); // p = [1,2], q = [1,null,2] // false
20+
const example3 = isSameTree(); // p = [1,2,1], q = [1,1,2] // false
21+
22+
console.log(example1);
23+
console.log(example2);
24+
console.log(example3);

example/6.Tree/0101.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 isSymmetric = function(root) {
14+
15+
};
16+
17+
const example1 = isSymmetric(); // [1,2,2,3,4,4,3] // true
18+
const example2 = isSymmetric(); // [1,2,2,null,3,null,3] // false
19+
const example3 = isSymmetric(); // [1,2,3,null,null,4] // 3
20+
const example4 = isSymmetric(); // [] // 0
21+
22+
console.log(example1);
23+
console.log(example2);
24+
console.log(example3);
25+
console.log(example4);

example/6.Tree/0102.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
15+
};
16+
17+
const example1 = levelOrder(); // root = [3,9,20,null,null,15,7] // [[3],[9,20],[15,7]]
18+
const example2 = levelOrder(); // root = [1] // [[1]]
19+
const example3 = levelOrder(); // root = [] // []
20+
const example4 = levelOrder(); // root = [1,2,3,4,5,6,7] // [[1],[2,3],[4,5,6,7]]
21+
22+
console.log(example1);
23+
console.log(example2);
24+
console.log(example3);
25+
console.log(example4);

example/6.Tree/0103.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 zigzagLevelOrder = function(root) {
14+
15+
};
16+
17+
const example1 = zigzagLevelOrder(); // root = [3,9,20,null,null,15,7] // [[3],[20,9],[15,7]]
18+
const example2 = zigzagLevelOrder(); // root = [[1] // [[1]]
19+
const example3 = zigzagLevelOrder(); // root = [] // []
20+
21+
console.log(example1);
22+
console.log(example2);
23+
console.log(example3);

example/6.Tree/0104.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
15+
};
16+
17+
const example1 = maxDepth(); // [3,9,20,null,null,15,7] // 3
18+
const example2 = maxDepth(); // [1,null,2] // 2
19+
const example3 = maxDepth(); // [1,2,3,null,null,4] // 3
20+
const example4 = maxDepth(); // [] // 0
21+
22+
console.log(example1);
23+
console.log(example2);
24+
console.log(example3);
25+
console.log(example4);

example/6.Tree/0110.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
15+
};
16+
17+
const example1 = isBalanced(); // root = [3,9,20,null,null,15,7] // true
18+
const example2 = isBalanced(); // root = [1,2,2,3,3,null,null,4,4] // false
19+
const example3 = isBalanced(); // root = [] // true
20+
const example4 = isBalanced(); // root = [1,2,3,null,null,4] // true
21+
const example5 = isBalanced(); // root = [1,2,3,null,null,4,null,5] // false
22+
23+
console.log(example1);
24+
console.log(example2);
25+
console.log(example3);
26+
console.log(example4);
27+
console.log(example5);

example/6.Tree/0112.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 {number} targetSum
12+
* @return {boolean}
13+
*/
14+
var hasPathSum = function(root, targetSum) {
15+
16+
};
17+
18+
const example1 = hasPathSum(); // root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 // true
19+
const example2 = hasPathSum(); // root = [1,2,3], targetSum = 5 // false
20+
const example3 = hasPathSum(); // root = [], targetSum = 0 // false
21+
22+
console.log(example1);
23+
console.log(example2);
24+
console.log(example3);

example/6.Tree/0117.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* // Definition for a _Node.
3+
* function _Node(val, left, right, next) {
4+
* this.val = val === undefined ? null : val;
5+
* this.left = left === undefined ? null : left;
6+
* this.right = right === undefined ? null : right;
7+
* this.next = next === undefined ? null : next;
8+
* };
9+
*/
10+
11+
/**
12+
* @param {_Node} root
13+
* @return {_Node}
14+
*/
15+
var connect = function(root) {
16+
17+
};
18+
19+
const example1 = connect(); // root = [1,2,3,4,5,null,7] // [1,#,2,3,#,4,5,7,#]
20+
const example2 = connect(); // root = [] // []
21+
22+
console.log(example1);
23+
console.log(example2);

0 commit comments

Comments
 (0)