# Medium

{% code title="2. Add Two Numbers" %}

```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        dummy = node = ListNode(0)
        carry = 0
        while l2 or l1 or carry:
            x_val = l1.val if l1 else 0
            y_val = l2.val if l2 else 0
            s = x_val + y_val + carry
            if s >= 10:
                s -= 10
                carry = 1
            else:
                carry = 0
            node.next = ListNode(s)
            l1 = l1 and l1.next
            l2 = l2 and l2.next
            node = node.next
        return dummy.next
```

{% endcode %}

{% code title="144. Binary Tree Preorder Traversal" %}

```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)
```

{% endcode %}

{% code title="144. Binary Tree Preorder Traversal" %}

```python
class Solution:
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        ans, node = [],[root]
        while node:
            now = node.pop()
            if now:
                ans.append(now.val)
                node.extend([now.right, now.left])
```

{% endcode %}

{% code title="94. Binary Tree Inorder Traversal" %}

```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        result = []
        if root is not None:
            stack = []
            while stack or root is not None:
                if root is not None:
                    stack.append(root)
                    root = root.left
                else:
                    root = stack.pop()
                    result.append(root.val)
                    root = root.right
        return result
```

{% endcode %}

{% code title="3. Longest Substring Without Repeating Characters" %}

```c
int lengthOfLongestSubstring(char* s) {
    int map[256] = {0};
    int result = 0;
    int i = 0;
    int left = 0;
    while(*(s+i)!='\0'){
        if(map[*(s+i)] == 0 || map[*(s+i)]<left){
            map[*(s+i)] = i+1;
        }else{
            left = map[*(s+i)];
            map[*(s+i)] = i+1;
        }
        result = (i-left+1)>result? (i-left+1): result;
        i++;
    }
    return result;
}
```

{% endcode %}

{% code title="338. Counting Bits" %}

```python
class Solution:
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        ans = [0]
        for x in range(1, num + 1):
            ans += ans[x >> 1] + (x & 1),
        return ans
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://home2asa.gitbook.io/asa2leetcode/medium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
