-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...ePlayground.playground/Pages/95_UniqueBinarySearchTreesII.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//: [Previous](@previous) | ||
|
||
import Foundation | ||
|
||
|
||
/// Definition for a binary tree node. | ||
public class TreeNode { | ||
public var val: Int | ||
public var left: TreeNode? | ||
public var right: TreeNode? | ||
public init() { self.val = 0; self.left = nil; self.right = nil; } | ||
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } | ||
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
} | ||
} | ||
|
||
class Solution { | ||
func generateTrees(_ n: Int) -> [TreeNode?] { | ||
let res = [TreeNode?]() | ||
if n == 0 { | ||
return res | ||
} | ||
return _generateTrees(start: 1, end: n) | ||
|
||
} | ||
|
||
private func _generateTrees(start:Int, end:Int) -> [TreeNode?]{ | ||
var res = [TreeNode?]() | ||
if start > end { | ||
res.append(nil) | ||
return res | ||
} | ||
if start == end { | ||
let tmp = TreeNode(start) | ||
res.append(tmp) | ||
return res | ||
} | ||
for i in start...end { | ||
let leftTrees = _generateTrees(start: start, end: i - 1) | ||
let rightTrees = _generateTrees(start: i + 1, end: end) | ||
for tmpLeft in leftTrees { | ||
for tmpRight in rightTrees { | ||
let root = TreeNode(i) | ||
root.left = tmpLeft | ||
root.right = tmpRight | ||
res.append(root) | ||
} | ||
} | ||
} | ||
return res | ||
} | ||
} | ||
|
||
//: [Next](@next) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters