Skip to content

Commit 521ec62

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
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);

example/6.Tree/0199.js

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

example/6.Tree/0226.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 {TreeNode}
12+
*/
13+
var invertTree = function(root) {
14+
15+
};
16+
17+
const example1 = invertTree(); // [4,2,7,1,3,6,9] //[ 4,7,2,9,6,3,1]
18+
const example2 = invertTree(); // [2,1,3] // [2,3,1]
19+
const example3 = invertTree(); // [] // []
20+
const example4 = invertTree(); // [1,2,3,4,5,6,7] // [1,3,2,7,6,5,4]
21+
const example5 = invertTree(); // [3,2,1] // [3,1,2]
22+
23+
console.log(example1);
24+
console.log(example2);
25+
console.log(example3);
26+
console.log(example4);
27+
console.log(example5);

example/6.Tree/0236.js

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) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function(root, p, q) {
15+
16+
};
17+
18+
const example1 = lowestCommonAncestor(); // root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 // 3
19+
const example2 = lowestCommonAncestor(); // root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 // 5
20+
const example3 = lowestCommonAncestor(); // root = [1,2], p = 1, q = 2 // 1
21+
const example4 = lowestCommonAncestor(); // root = [5,3,8,1,4,7,9,null,2], p = 3, q = 8 // 5
22+
const example5 = lowestCommonAncestor(); // root = [5,3,8,1,4,7,9,null,2], p = 3, q = 4 // 3
23+
24+
console.log(example1);
25+
console.log(example2);
26+
console.log(example3);
27+
console.log(example4);
28+
console.log(example5);

example/6.Tree/0515.js

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

example/6.Tree/0543.js

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

0 commit comments

Comments
 (0)