|
94 | 94 | "source": [
|
95 | 95 | "# Definition for a binary tree node.\n",
|
96 | 96 | "class TreeNode(object):\n",
|
97 |
| - " def __init__(self, val = 0, left = None, right = None):\n", |
| 97 | + " def __init__(self, val=0, left=None, right=None):\n", |
98 | 98 | " self.val = val\n",
|
99 | 99 | " self.left = left\n",
|
100 | 100 | " self.right = right\n",
|
101 | 101 | "\n",
|
102 |
| - "# Building the tree(DFS-pre-order)\n", |
| 102 | + "# Building the tree(DFS_pre-order)\n", |
103 | 103 | "def build_tree(roots):\n",
|
104 | 104 | " if not roots:\n",
|
105 | 105 | " return None\n",
|
106 |
| - " \n", |
| 106 | + "\n", |
107 | 107 | " root = TreeNode(roots[0])\n",
|
108 | 108 | " built_tree = [root]\n",
|
109 | 109 | " index = 1\n",
|
110 |
| - " \n", |
| 110 | + "\n", |
111 | 111 | " while built_tree and index < len(roots):\n",
|
112 | 112 | " node = built_tree.pop(0)\n",
|
113 |
| - " \n", |
| 113 | + "\n", |
114 | 114 | " if index < len(roots) and roots[index] is not None:\n",
|
115 | 115 | " node.left = TreeNode(roots[index])\n",
|
116 | 116 | " built_tree.append(node.left)\n",
|
117 | 117 | " index += 1\n",
|
118 |
| - " \n", |
| 118 | + "\n", |
119 | 119 | " if index < len(roots) and roots[index] is not None:\n",
|
120 | 120 | " node.right = TreeNode(roots[index])\n",
|
121 | 121 | " built_tree.append(node.right)\n",
|
122 | 122 | " index += 1\n",
|
123 |
| - " \n", |
| 123 | + "\n", |
124 | 124 | " return root\n",
|
125 | 125 | "\n",
|
126 |
| - "def dfs(node, depth, visited):\n", |
| 126 | + "# DFS function to find duplicates and their depths\n", |
| 127 | + "def dfs(node, depth, visited, closest_duplicate, min_depth):\n", |
127 | 128 | " if node is None:\n",
|
128 |
| - " return None, float('inf') # No duplicate, infinite depth\n", |
129 |
| - "\n", |
130 |
| - " # If the node's value has already been visited, it's a duplicate\n", |
| 129 | + " return closest_duplicate, min_depth # No duplicates found in this path\n", |
| 130 | + " \n", |
131 | 131 | " if node.val in visited:\n",
|
132 |
| - " return node.val, depth # Found a duplicate at this depth\n", |
| 132 | + " if depth < min_depth:\n", |
| 133 | + " closest_duplicate = node.val\n", |
| 134 | + " min_depth = depth\n", |
133 | 135 | " \n",
|
134 | 136 | " visited.add(node.val)\n",
|
135 | 137 | "\n",
|
136 |
| - " # Recurse on left and right children and return the closest duplicate\n", |
137 |
| - " left_val, left_depth = dfs(node.left, depth + 1, visited)\n", |
138 |
| - " right_val, right_depth = dfs(node.right, depth + 1, visited)\n", |
| 138 | + " closest_duplicate, min_depth = dfs(node.left, depth + 1, visited, closest_duplicate, min_depth)\n", |
| 139 | + " closest_duplicate, min_depth = dfs(node.right, depth + 1, visited, closest_duplicate, min_depth)\n", |
139 | 140 | "\n",
|
140 |
| - " # Compare which duplicate is closer to the root\n", |
141 |
| - " if left_depth < right_depth:\n", |
142 |
| - " return left_val, left_depth\n", |
143 |
| - " else:\n", |
144 |
| - " return right_val, right_depth\n", |
| 141 | + " return closest_duplicate, min_depth\n", |
145 | 142 | "\n",
|
146 | 143 | "\n",
|
| 144 | + "# Function to find the duplicate with the smallest depth (closest to the root)\n", |
147 | 145 | "def find_duplicate(root):\n",
|
148 |
| - " visited = set() # To track visited node values\n", |
149 |
| - " closest_duplicate, min_depth = dfs(root, 0, visited)\n", |
150 |
| - " \n", |
151 |
| - " # Return the closest duplicate if found, otherwise return -1\n", |
152 |
| - " return closest_duplicate if closest_duplicate is not None else -1\n", |
153 |
| - "\n" |
| 146 | + " visited = set() \n", |
| 147 | + " closest_duplicate, min_depth = dfs(root, 0, visited, None, float('inf')) # setting root as the node(depth 0),closest_duplicate as None, and min_depth as infinite as everything will be less than that\n", |
| 148 | + "\n", |
| 149 | + " return closest_duplicate if closest_duplicate is not None else -1\n" |
154 | 150 | ]
|
155 | 151 | },
|
156 | 152 | {
|
157 | 153 | "cell_type": "code",
|
158 |
| - "execution_count": 21, |
| 154 | + "execution_count": 45, |
159 | 155 | "metadata": {},
|
160 | 156 | "outputs": [
|
161 | 157 | {
|
162 | 158 | "name": "stdout",
|
163 | 159 | "output_type": "stream",
|
164 | 160 | "text": [
|
165 |
| - "Duplicate value: 2\n" |
| 161 | + "Duplicate value: -1\n" |
166 | 162 | ]
|
167 | 163 | }
|
168 | 164 | ],
|
169 | 165 | "source": [
|
170 |
| - "example_roots = [1, 2, 2, 3, 5, 6, 7] # Testing the example 1\n", |
| 166 | + "example_roots = [1, 2, 2, 3, 5, 6, 7] # Testing example 1\n", |
171 | 167 | "root = build_tree(example_roots)\n",
|
172 | 168 | "duplicate = find_duplicate(root)\n",
|
173 | 169 | "print(f\"Duplicate value: {duplicate}\") "
|
174 | 170 | ]
|
175 | 171 | },
|
176 | 172 | {
|
177 | 173 | "cell_type": "code",
|
178 |
| - "execution_count": 22, |
| 174 | + "execution_count": 39, |
179 | 175 | "metadata": {},
|
180 | 176 | "outputs": [
|
181 | 177 | {
|
182 | 178 | "name": "stdout",
|
183 | 179 | "output_type": "stream",
|
184 | 180 | "text": [
|
185 |
| - "Duplicate value: 12\n" |
| 181 | + "Duplicate value: 10\n" |
| 182 | + ] |
| 183 | + } |
| 184 | + ], |
| 185 | + "source": [ |
| 186 | + "example_roots = [1, 10, 2, 3, 10, 12, 12] # Testing example 2\n", |
| 187 | + "root = build_tree(example_roots)\n", |
| 188 | + "duplicate = find_duplicate(root)\n", |
| 189 | + "print(f\"Duplicate value: {duplicate}\") " |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": 40, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [ |
| 197 | + { |
| 198 | + "name": "stdout", |
| 199 | + "output_type": "stream", |
| 200 | + "text": [ |
| 201 | + "Duplicate value: -1\n" |
186 | 202 | ]
|
187 | 203 | }
|
188 | 204 | ],
|
189 | 205 | "source": [
|
190 |
| - "example_roots = [1, 10, 2, 3, 10, 12, 12] # Testing the example 3\n", |
| 206 | + "example_roots = [10, 9, 8, 7] # Testing example 3\n", |
191 | 207 | "root = build_tree(example_roots)\n",
|
192 | 208 | "duplicate = find_duplicate(root)\n",
|
193 | 209 | "print(f\"Duplicate value: {duplicate}\") "
|
194 | 210 | ]
|
195 | 211 | },
|
196 | 212 | {
|
197 | 213 | "cell_type": "code",
|
198 |
| - "execution_count": 17, |
| 214 | + "execution_count": 41, |
199 | 215 | "metadata": {},
|
200 | 216 | "outputs": [
|
201 | 217 | {
|
|
0 commit comments