File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ const MyCalendar = function ( ) {
2
+ this . root = null
3
+ }
4
+
5
+ const Node = function ( start , end ) {
6
+ this . start = start
7
+ this . end = end
8
+ this . left = null
9
+ this . right = null
10
+ }
11
+
12
+ Node . prototype . insert = function ( node ) {
13
+ if ( node . start >= this . end ) {
14
+ if ( this . right === null ) {
15
+ this . right = node
16
+ return true
17
+ }
18
+ return this . right . insert ( node )
19
+ } else if ( node . end <= this . start ) {
20
+ if ( this . left === null ) {
21
+ this . left = node
22
+ return true
23
+ }
24
+ return this . left . insert ( node )
25
+ } else {
26
+ return false
27
+ }
28
+ }
29
+
30
+ /**
31
+ * @param {number } start
32
+ * @param {number } end
33
+ * @return {boolean }
34
+ */
35
+ MyCalendar . prototype . book = function ( start , end ) {
36
+ const newNode = new Node ( start , end )
37
+ if ( this . root === null ) {
38
+ this . root = newNode
39
+ return true
40
+ } else {
41
+ return this . root . insert ( newNode )
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Your MyCalendar object will be instantiated and called as such:
47
+ * var obj = new MyCalendar()
48
+ * var param_1 = obj.book(start,end)
49
+ */
You can’t perform that action at this time.
0 commit comments