Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2858c38

Browse files
committed
add more solutions (up to #145)
1 parent 1b5678e commit 2858c38

36 files changed

+1182
-0
lines changed

solutions/100_SameTree.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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} p
10+
* @param {TreeNode} q
11+
* @return {boolean}
12+
*/
13+
var isSameTree = function(p, q) {
14+
//Recursive solution
15+
// function helper(p, q){
16+
// if(!p && !q){
17+
// return true
18+
// }
19+
// if(!p || !q){
20+
// return false
21+
// }
22+
// if(p.val != q.val){
23+
// return false
24+
// }
25+
// return helper(p.left,q.left) && helper(p.right,q.right)
26+
// }
27+
// return helper(p,q)
28+
var queue = [[p,q]];
29+
function check(p,q){
30+
if(!p && !q){
31+
return true;
32+
}
33+
if(!p || !q){
34+
return false
35+
}
36+
if(p.val != q.val){
37+
return false
38+
}
39+
return true;
40+
}
41+
while(queue.length > 0){
42+
let [p, q] = queue.pop();
43+
if(!check(p,q)){
44+
return false;
45+
}
46+
if(p){
47+
queue.push([p.left, q.left]);
48+
queue.push([p.right, q.right]);
49+
}
50+
}
51+
return true;
52+
};

solutions/101_SymmetricTree.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* @return {boolean}
11+
*/
12+
var isSymmetric = function(root) {
13+
function helper(left, right){
14+
if(!left && !right){
15+
return true
16+
}
17+
if(!left || !right){
18+
return false
19+
}
20+
if(left.val == right.val){
21+
let n_left = helper(left.left, right.right)
22+
let n_right = helper(left.right, right.left)
23+
return n_left && n_right
24+
} else {
25+
return false
26+
}
27+
}
28+
if(!root){
29+
return true
30+
}
31+
return helper(root.left,root.right)
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* @return {number[][]}
11+
*/
12+
var levelOrder = function(root) {
13+
// so okay, we should go from top to bottom, from left to write.
14+
// until our queue is full get data from it and put it to result. (add data to queue from right to left)
15+
// when queue is empty add childs from roots
16+
var result = []
17+
function helper(node,level){
18+
if(!node) {
19+
return;
20+
}
21+
if(!result[level]) {
22+
result[level] = []
23+
}
24+
result[level].push(node.val);
25+
helper(node.left,level+1);
26+
helper(node.right,level+1);
27+
}
28+
helper(root,0)
29+
return result;
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[List[int]]
13+
"""
14+
result = []
15+
#self.bfs_rec(root,0, result)
16+
self.bfs_iter(root,result)
17+
return result
18+
19+
def bfs_rec(self, root, level, result):
20+
if not root:
21+
return;
22+
if level < len(result):
23+
result.append([])
24+
25+
result[level].append(root.val)
26+
self.bfs_rec(root.left, level + 1, result)
27+
self.bfs_rec(root.right, level+1, result)
28+
29+
def bfs_iter(self, root, result):
30+
if not root:
31+
return
32+
queue = [root]
33+
34+
while queue:
35+
tmp = []
36+
for _ in range(len(queue)):
37+
node = queue.pop(0)
38+
if node.left:
39+
queue.append(node.left)
40+
if node.right:
41+
queue.append(node.right)
42+
tmp.append(node.val)
43+
result.append(tmp)
44+
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
* @return {number}
11+
*/
12+
var maxDepth = function(root) {
13+
function helper(head,acc){
14+
if(!head){
15+
return acc+0
16+
}
17+
if(!head.left && !head.right){
18+
return acc+1
19+
}
20+
return Math.max(helper(head.left, acc+1),helper(head.right,acc+1))
21+
}
22+
23+
return helper(root,0)
24+
};
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def maxDepth(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
return self.dfs_max(root,0)
15+
16+
def dfs_max(self, root, accum):
17+
if not root:
18+
return accum
19+
if not root.left and not root.right:
20+
return accum+1
21+
return max(self.dfs_max(root.left,accum+1), self.dfs_max(root.right, accum+1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* @return {number[][]}
11+
*/
12+
var levelOrderBottom = function(root) {
13+
var result = [];
14+
var queue = [root];
15+
if(!root){
16+
return []
17+
}
18+
while(queue.length != 0){
19+
let size = queue.length;
20+
let tmp = []
21+
for(let i = 0;i<size;i++){
22+
let node = queue.shift();
23+
tmp.push(node.val);
24+
if(node.left){queue.push(node.left)}
25+
if(node.right){queue.push(node.right)}
26+
}
27+
result.push(tmp)
28+
}
29+
return result.reverse()
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 {number[]} nums
10+
* @return {TreeNode}
11+
*/
12+
var sortedArrayToBST = function(nums) {
13+
function helper(nums){
14+
if(nums.length == 0){
15+
return null
16+
}
17+
let half = Math.floor(nums.length / 2);
18+
let root = new TreeNode(nums[half])
19+
root.left = helper(nums.slice(0,half))
20+
//because we are using half as Value.
21+
root.right = helper(nums.slice(half+1))
22+
return root
23+
}
24+
return helper(nums)
25+
};

solutions/110_BalancedBinaryTree.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
* @return {boolean}
11+
*/
12+
var isBalanced = function(root) {
13+
function helper(root){
14+
if(!root){
15+
return 0;
16+
}
17+
18+
let left = helper(root.left);
19+
let right = helper(root.right);
20+
if(left == -1 || right == -1 || Math.abs(left-right) > 1){
21+
return -1;
22+
}
23+
return 1 + Math.max(left,right)
24+
}
25+
return helper(root) != -1
26+
};
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
* @return {number}
11+
*/
12+
var minDepth = function(root) {
13+
function dfs(root, count){
14+
if(!root){
15+
return 0;
16+
}
17+
if(!root.left || !root.right){
18+
return Math.max(dfs(root.left, count+1),dfs(root.right, count+1)) + 1
19+
}
20+
if(root){
21+
return Math.min(dfs(root.left, count+1),dfs(root.right, count+1)) + 1
22+
}
23+
}
24+
return dfs(root, 0);
25+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def minDepth(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
return self.min(root)
15+
16+
def min(self, root):
17+
if not root:
18+
return 0
19+
if not root.left or not root.right:
20+
return max(self.min(root.left), self.min(root.right)) + 1
21+
if root:
22+
return min(self.min(root.left), self.min(root.right)) + 1
23+

solutions/112_PathSum.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 {number} sum
11+
* @return {boolean}
12+
*/
13+
var hasPathSum = function(root, sum) {
14+
function helper(node, sum){
15+
if(!node){
16+
return false;
17+
}
18+
if(!node.left && !node.right && sum == node.val){
19+
return true
20+
}
21+
let left = helper(node.left, sum-node.val);
22+
let right = helper(node.right, sum-node.val);
23+
return left || right
24+
}
25+
let result = helper(root, sum);
26+
return result;
27+
};

0 commit comments

Comments
 (0)