```
class Solution:
def findMaxK(self, nums: List[int]) -> int:
nums.sort()
p1 = 0
p2 = len(nums)-1
while p1 < p2:
l = nums[p1]
r = nums[p2]
ssum = l + r
if ssum == 0:
return r
if ssum < 0:
p1 += 1
else:
p2 -= 1
return -1
```

```
class Solution:
def findMaxK(self, nums: List[int]) -> int:
s = set(nums)
return max((x for x in s if -x in s), default=-1)
```

Use a depth-first search (DFS) approach to traverse from leaves to the root, collecting characters, and keep track of the smallest string found.

Tackle by recursively exploring each path from the root to the leaves, collecting the string formed by node values in reverse (from leaf to root). At each leaf node, compare the formed string with the current smallest and update if the new one is smaller.

- Create a recursive function
`dfs(node, path)`

that navigates through the tree:- The
`path`

argument accumulates characters from the current node to the root. - At each leaf node (node with no children), update the smallest string.
- Recursively visit left and right children if they exist.

- The
- Start the DFS with the root node and an empty path.
- After traversing the entire tree, the smallest string will be the result.

```
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def smallestFromLeaf(self, root: TreeNode) -> str:
nonlocal smallest
if node:
# Prepend current char to the path
path = chr(node.val + 97) + path
if not node.left and not node.right: # Leaf node
if not smallest or path < smallest:
smallest = path
dfs(node.left, path)
dfs(node.right, path)
smallest = None
dfs(root, "")
return smallest
```

Traverse the tree and check for leaf nodes specifically that are left children. Consider using a depth-first search (DFS) approach.

The strategy is to recursively traverse the tree, and at each node, check if it has a left child that is a leaf. If it is, we add its value to the sum. We continue traversing until all nodes are visited.

- Define a helper function
`dfs(node)`

that will traverse the tree:- Check if the current node is None; if yes, return 0.
- Check if the left child of the node is a leaf (i.e., has no left or right child). If it is, add its value to the sum.
- Recursively call the helper for the left and right children of the current node.
- Return the sum of values from the left and right child calls plus the leaf node value if applicable.

- Call this
`dfs`

function starting from the root of the tree.

```
class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def dfs(node):
if not node:
return 0
res = 0
if node.left and not node.left.left and not node.left.right:
res += node.left.val
res += dfs(node.left)
res += dfs(node.right)
return res
return dfs(root)
```

```
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def dfs(root, sum_so_far):
if root is None:
return 0
res = 0
sum_so_far = sum_so_far * 10 + root.val
if root.left is None and root.right is None:
return sum_so_far
res += dfs(root.left, sum_so_far)
res += dfs(root.right, sum_so_far)
return res
res = dfs(root, 0)
return res
```

Consider using a depth-first search (DFS) approach

The strategy is to recursively traverse the tree, and at each node, check if it has a left child that is a leaf. If it is, we add its value to the sum. We continue traversing until all nodes are visited.

- Define a helper function
`dfs(node)`

that will traverse the tree:- Check if the current node is None; if yes, return 0.
- Check if the left child of the node is a leaf (i.e., has no left or right child). If it is, add its value to the sum.
- Recursively call the helper for the left and right children of the current node.
- Return the sum of values from the left and right child calls plus the leaf node value if applicable.

- Call this dfs function starting from the root of the tree.

```
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sumOfLeftLeaves(root):
def dfs(node):
if not node:
return 0
sum = 0
if node.left and not node.left.left and not node.left.right:
sum += node.left.val
sum += dfs(node.left)
sum += dfs(node.right)
return sum
return dfs(root)
```

```
class Solution:
sum = 0
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def dfs(root, is_left=False):
if not root:
return 0
if is_left:
if not root.left and not root.right:
self.sum += root.val
dfs(root.left, True)
dfs(root.right)
dfs(root)
return self.sum
```

```
class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
if root is None:
return 0
res = 0
if root.left and root.left.left is None and root.left.right is None:
res += root.left.val
res += self.sumOfLeftLeaves(root.left)
res += self.sumOfLeftLeaves(root.right)
return res
```

```
class Solution:
def maxDepth(self, s: str) -> int:
stack = []
res = 0
for c in s:
if c == '(':
stack.append(1)
res = max(res, len(stack))
elif c == ')':
stack.pop()
return res
```

Use two dictionaries to track the mappings from `s`

to `t`

and from `t`

to `s`

. This helps in ensuring no two characters map to the same character.

**Key Idea**: To verify if two strings are isomorphic, we need to ensure that each character in s maps to a unique character in t, and vice versa. This bi-directional mapping is crucial to maintain the isomorphism property.**Data Structures**: Two hash maps (or dictionaries in Python) are ideal for maintaining these mappings efficiently.

- Initial Checks: If the lengths of
`s`

and`t`

are different, they cannot be isomorphic. Return false immediately. - Create Two Mappings: Initialize two dictionaries. One for mapping characters from
`s`

to`t`

(`s_to_t`

) and another from`t`

to`s`

(`t_to_s`

). - Iterate Over Characters:

- For each character pair (
`s_char`

,`t_char`

) in`s`

and`t`

, check:- If
`s_char`

maps to a different`t_char`

in`s_to_t`

, or`t_char`

maps to a different`s_char`

in`t_to_s`

, return false. - Update
`s_to_t[s_char] = t_char`

and`t_to_s[t_char] = s_char`

.

- If

- Return True: If the loop completes without returning false, then
`s`

and`t`

are isomorphic.

```
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
s_to_t, t_to_s = {}, {}
for s_char, t_char in zip(s, t):
if (s_char in s_to_t and s_to_t[s_char] != t_char) or \
(t_char in t_to_s and t_to_s[t_char] != s_char):
return False
s_to_t[s_char] = t_char
t_to_s[t_char] = s_char
return True
```

```
class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
j1 = j2 = k = -1
res = 0
for i, v in enumerate(nums):
if v < minK or v > maxK:
k = i
if v == minK:
j1 = i
if v == maxK:
j2 = i
res += max(0, min(j1, j2) - k)
return res
```