Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06e46ff

Browse files
Day 92
1 parent 30e967d commit 06e46ff

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import UIKit
2+
3+
/*
4+
Two elements of a binary search tree (BST) are swapped by mistake.
5+
6+
Recover the tree without changing its structure.
7+
8+
Example 1:
9+
10+
Input: [1,3,null,null,2]
11+
12+
1
13+
/
14+
3
15+
\
16+
2
17+
18+
Output: [3,1,null,null,2]
19+
20+
3
21+
/
22+
1
23+
\
24+
2
25+
Example 2:
26+
27+
Input: [3,1,4,null,null,2]
28+
29+
3
30+
/ \
31+
1 4
32+
/
33+
2
34+
35+
Output: [2,1,4,null,null,3]
36+
37+
2
38+
/ \
39+
1 4
40+
/
41+
3
42+
Follow up:
43+
44+
A solution using O(n) space is pretty straight forward.
45+
Could you devise a constant space solution?
46+
*/
47+
48+
/**
49+
* Definition for a binary tree node.
50+
*/
51+
public class TreeNode {
52+
public var val: Int
53+
public var left: TreeNode?
54+
public var right: TreeNode?
55+
public init() { self.val = 0; self.left = nil; self.right = nil; }
56+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
57+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
58+
self.val = val
59+
self.left = left
60+
self.right = right
61+
}
62+
}
63+
64+
class Solution {
65+
var first: TreeNode?
66+
var second: TreeNode?
67+
var prev = TreeNode(Int.min)
68+
69+
func recoverTree(_ root: TreeNode?) {
70+
inOrder(root)
71+
72+
let temp = first!.val
73+
first!.val = second!.val
74+
second!.val = temp
75+
}
76+
77+
func inOrder(_ node: TreeNode?) {
78+
guard let node = node else {return}
79+
80+
inOrder(node.left)
81+
82+
if first == nil && prev.val >= node.val {
83+
first = prev
84+
}
85+
if first != nil && prev.val >= node.val {
86+
second = node
87+
}
88+
prev = node
89+
90+
inOrder(node.right)
91+
}
92+
}
93+
94+
import Foundation
95+
import XCTest
96+
class SolutionTests: XCTestCase {
97+
var solution: Solution!
98+
override func setUp() {
99+
super.setUp()
100+
solution = Solution()
101+
}
102+
103+
func test1() {
104+
// let results = solution.threeSum([-1, 1])
105+
// XCTAssertEqual(results, [])
106+
}
107+
}
108+
109+
SolutionTests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

‎99. Recover Binary Search Tree.playground/playground.xcworkspace/contents.xcworkspacedata‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "file:///Users/zenglekidd/Desktop/99.%20Recover%20Binary%20Search%20Tree.playground#CharacterRangeLen=1086&amp;CharacterRangeLoc=509&amp;EndingColumnNumber=1&amp;EndingLineNumber=91&amp;StartingColumnNumber=0&amp;StartingLineNumber=47&amp;Timestamp=624204658.73497"
7+
selectedRepresentationIndex = "0"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
</TimelineItems>
11+
</Timeline>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /