Skip to content

Commit 548a8b8

Browse files
authored
Merge pull request algorithm004-01#365 from pauleyliu/master
551_Week 02
2 parents 6626d6e + 27352c9 commit 548a8b8

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init(_ val: Int) {
8+
* self.val = val
9+
* self.left = nil
10+
* self.right = nil
11+
* }
12+
* }
13+
*/
14+
class Solution {
15+
func maxDepth(_ root: TreeNode?) -> Int {
16+
guard let r = root else { return 0 }
17+
// handle exponentially more recursive call to null root nodes
18+
let l_h = (r.left == nil) ? 0 : maxDepth(r.left)
19+
let r_h = (r.right == nil) ? 0 : maxDepth(r.right)
20+
return (l_h > r_h ? l_h : r_h) + 1
21+
}
22+
}
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+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init(_ val: Int) {
8+
* self.val = val
9+
* self.left = nil
10+
* self.right = nil
11+
* }
12+
* }
13+
*/
14+
class Solution {
15+
func minDepth(_ root: TreeNode?) -> Int {
16+
guard let r = root else { return 0 }
17+
18+
let left_h = minDepth(r.left)
19+
let right_h = minDepth(r.right)
20+
21+
if r.left == nil || r.right == nil {
22+
// leaf node、left null right node、right null left node
23+
// 0 0, 0 r_h, l_h 0
24+
return left_h + right_h + 1
25+
} else {
26+
return (left_h > right_h ? right_h : left_h) + 1
27+
}
28+
}
29+
}

Week 02/id_551/LeetCode_226_551.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def invertTree(self, root: TreeNode) -> TreeNode:
10+
if root is not None:
11+
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
12+
return root
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init(_ val: Int) {
8+
* self.val = val
9+
* self.left = nil
10+
* self.right = nil
11+
* }
12+
* }
13+
*/
14+
class Solution {
15+
func invertTree(_ root: TreeNode?) -> TreeNode? {
16+
if let r = root { (r.left, r.right) = (invertTree(r.right), invertTree(r.left)) }
17+
return root
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
iclass Solution {
2+
func climbStairs(_ n: Int) -> Int {
3+
if n <= 1 { return 1 }
4+
5+
var dp: [Int] = Array(repeating: 0, count: n + 1)
6+
dp[0] = 1
7+
dp[1] = 1
8+
9+
for i in 2...n {
10+
dp[i] = dp[i-1] + dp[i-2]
11+
}
12+
13+
return dp[n]
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
3+
var cache = [Int: Int]()
4+
func climbStairs(_ n: Int) -> Int {
5+
6+
if n <= 1 { return 1 }
7+
8+
if let val = cache[n] {
9+
return val
10+
} else {
11+
cache[n] = climbStairs(n - 1) + climbStairs(n - 2)
12+
return cache[n]!
13+
}
14+
15+
}
16+
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init(_ val: Int) {
8+
* self.val = val
9+
* self.left = nil
10+
* self.right = nil
11+
* }
12+
* }
13+
*/
14+
class Solution {
15+
func isValidBST(_ root: TreeNode?) -> Bool {
16+
17+
// recursion func
18+
func isValid(_ root: TreeNode?, lower: Int = Int.min, higher: Int = Int.max) -> Bool {
19+
20+
// recursion terminator
21+
guard let r = root else { return true }
22+
23+
// process current level
24+
if r.val <= lower || r.val >= higher { return false }
25+
26+
// drill down (boundary convergence)
27+
if !isValid(r.left, lower: lower, higher: r.val) { return false }
28+
if !isValid(r.right, lower: r.val, higher: higher) { return false }
29+
30+
return true
31+
}
32+
33+
return isValid(root)
34+
}
35+
}
36+
37+
38+

0 commit comments

Comments
 (0)