You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.
5
+
6
+
Example 1:
7
+
Input: root = [1,3,null,null,2]
8
+
Output: [3,1,null,null,2]
9
+
Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid.
10
+
11
+
Example 2:
12
+
Input: root = [3,1,4,null,null,2]
13
+
Output: [2,1,4,null,null,3]
14
+
Explanation: 2 cannot be in the right subtree of 3 because 2 < 3. Swapping 2 and 3 makes the BST valid.
15
+
16
+
Constraints:
17
+
The number of nodes in the tree is in the range [2, 1000].
18
+
-231 <= Node.val <= 231 - 1
19
+
20
+
Follow up: A solution using O(n) space is pretty straight-forward. Could you devise a constant O(1) space solution?
We are traversing the tree rooted at root in O(N) time. We are also traversing the tree rooted at subRoot in O(M) time. For each node, we are doing constant time operations. After traversing, for lookup we are either doing O(1) operations, or O(N) operations. Hence, the overall time complexity is O(N+M).
45
80
81
+
Space complexity: O(M+N).
82
+
We are using memo to store the hash pair of each node in the tree rooted at root. Hence, for this, we need O(N) space.
83
+
Moreover, since we are using recursion, the space required for the recursion stack will be O(N) for hashSubtreeAtNode(root, true) and O(M) for hashSubtreeAtNode(subRoot, false).
Putting the N stones of the input array into the bucket array is O(N), because inserting each stone is an O(1) operation.
85
+
In the worst case, the main loop iterates through all of the W indexes of the bucket array. Processing each bucket is an O(1) operation. This, therefore, is O(W).
86
+
Seeing as we don't know which is larger out of N and W, we get a total of O(N+W).
87
+
Technically, this algorithm is pseudo-polynomial, as its time complexity is dependent on the numeric value of the input.
88
+
Pseudo-polynomial algorithms are useful when there is no "true" polynomial alternative, but in situations such as this one where we have an O(NlogN) alternative (Approach 3), they are only useful for very specific inputs.
89
+
With the small values of W that your code is tested against for this question here on LeetCode, this approach turns out to be faster than Approach 3.
In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1.
5
+
For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4.
6
+
The twin sum is defined as the sum of a node and its twin.
7
+
Given the head of a linked list with even length, return the maximum twin sum of the linked list.
8
+
9
+
Example 1:
10
+
Input: head = [5,4,2,1]
11
+
Output: 6
12
+
Explanation:
13
+
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
14
+
There are no other nodes with twins in the linked list.
15
+
Thus, the maximum twin sum of the linked list is 6.
16
+
17
+
Example 2:
18
+
Input: head = [4,2,2,3]
19
+
Output: 7
20
+
Explanation:
21
+
The nodes with twins present in this linked list are:
22
+
- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
23
+
- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
24
+
Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
25
+
26
+
Example 3:
27
+
Input: head = [1,100000]
28
+
Output: 100001
29
+
Explanation:
30
+
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
31
+
32
+
33
+
Constraints:
34
+
The number of nodes in the list is an even integer in the range [2, 105].
35
+
1 <= Node.val <= 105
36
+
"""
37
+
38
+
# Definition for singly-linked list.
39
+
# class ListNode:
40
+
# def __init__(self, val=0, next=None):
41
+
# self.val = val
42
+
# self.next = next
43
+
classSolution:
44
+
defpairSum(self, head: Optional[ListNode]) ->int:
45
+
slow,fast=head,head
46
+
maxSum=0
47
+
48
+
whilefastandfast.next:
49
+
fast=fast.next.next
50
+
slow=slow.next
51
+
52
+
curr,prev=slow,None
53
+
whilecurr:
54
+
curr.next,prev,curr=prev,curr,curr.next
55
+
56
+
start=head
57
+
whileprev:
58
+
maxSum=max(maxSum,start.val+prev.val)
59
+
prev=prev.next
60
+
start=start.next
61
+
returnmaxSum
62
+
63
+
'''
64
+
Complexity Analysis
65
+
Here, n is the number of nodes in the linked list.
66
+
67
+
Time complexity: O(n)
68
+
69
+
It takes O(n) time to iterate over the linked list to find the middle and then reverse the second half of the linked list.
70
+
We iterate over the half of the linked list to find the maximum twin sum, which also takes O(n) time.
71
+
Space complexity: O(1)
72
+
73
+
Except for a few pointers that take up constant space, we don't take up any space.
0 commit comments