tinycodinglm / cp /cp-10010.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
1.92 kB
{"type": "session", "harness": "opencode", "id": "cp-10010", "name": "LeetCode 0501: Find Mode In Binary Search Tree"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Find Mode in Binary Search Tree (Difficulty: Easy)\nTags: Tree, Depth-First Search, Binary Search Tree, Binary Tree\n\nGiven the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.\nIf the tree has more than one mode, return them in any order.\nAssume a BST is defined as follows:\n\nExample 1:\nInput: root = [1,null,2,2]\nOutput: [2]\n\nExample 2:\nInput: root = [0]\nOutput: [0]\n\nConstraints:\n- The number of nodes in the tree is in the range [1, 104].\n- -10^5 <= Node.val <= 10^5"}}
{"type": "message", "message": {"role": "assistant", "content": "# Time: O(n)\n# Space: O(h)\n\nclass Solution(object):\n def findMode(self, root):\n \"\"\"\n :type root: TreeNode\n :rtype: List[int]\n \"\"\"\n def inorder(root, prev, cnt, max_cnt, result):\n if not root:\n return prev, cnt, max_cnt\n\n prev, cnt, max_cnt = inorder(root.left, prev, cnt, max_cnt, result)\n if prev:\n if root.val == prev.val:\n cnt += 1\n else:\n cnt = 1\n if cnt > max_cnt:\n max_cnt = cnt\n del result[:]\n result.append(root.val)\n elif cnt == max_cnt:\n result.append(root.val)\n return inorder(root.right, root, cnt, max_cnt, result)\n\n if not root:\n return []\n result = []\n inorder(root, None, 1, 0, result)\n return result\n\n"}}