|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": null, |
| 5 | + "execution_count": 1, |
6 | 6 | "id": "imports", |
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
|
14 | 14 | }, |
15 | 15 | { |
16 | 16 | "cell_type": "code", |
17 | | - "execution_count": null, |
| 17 | + "execution_count": 2, |
18 | 18 | "id": "setup", |
19 | 19 | "metadata": {}, |
20 | 20 | "outputs": [], |
|
28 | 28 | }, |
29 | 29 | { |
30 | 30 | "cell_type": "code", |
31 | | - "execution_count": null, |
| 31 | + "execution_count": 3, |
32 | 32 | "id": "execute", |
33 | 33 | "metadata": {}, |
34 | | - "outputs": [], |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "data": { |
| 37 | + "text/plain": [ |
| 38 | + "6" |
| 39 | + ] |
| 40 | + }, |
| 41 | + "execution_count": 3, |
| 42 | + "metadata": {}, |
| 43 | + "output_type": "execute_result" |
| 44 | + } |
| 45 | + ], |
35 | 46 | "source": [ |
36 | 47 | "root = TreeNode[int].from_list(root_list)\n", |
37 | | - "p = find_node(root, p_val)\n", |
38 | | - "q = find_node(root, q_val)\n", |
| 48 | + "assert root is not None\n", |
| 49 | + "p = root.find_node(p_val)\n", |
| 50 | + "q = root.find_node(q_val)\n", |
| 51 | + "assert p is not None and q is not None\n", |
39 | 52 | "result = Solution().lowest_common_ancestor(root, p, q)\n", |
40 | 53 | "result.val if result else None" |
41 | 54 | ] |
42 | 55 | }, |
43 | 56 | { |
44 | 57 | "cell_type": "code", |
45 | | - "execution_count": null, |
| 58 | + "execution_count": 5, |
| 59 | + "id": "d84494ee", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "data": { |
| 64 | + "text/html": [ |
| 65 | + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", |
| 66 | + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", |
| 67 | + " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", |
| 68 | + "<!-- Generated by graphviz version 13.1.2 (20250808.2320)\n", |
| 69 | + " -->\n", |
| 70 | + "<!-- Pages: 1 -->\n", |
| 71 | + "<svg width=\"278pt\" height=\"260pt\"\n", |
| 72 | + " viewBox=\"0.00 0.00 278.00 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", |
| 73 | + "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n", |
| 74 | + "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-256 274,-256 274,4 -4,4\"/>\n", |
| 75 | + "<!-- 0 -->\n", |
| 76 | + "<g id=\"node1\" class=\"node\">\n", |
| 77 | + "<title>0</title>\n", |
| 78 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"135\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n", |
| 79 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"135\" y=\"-228.95\" font-family=\"Times,serif\" font-size=\"14.00\">6</text>\n", |
| 80 | + "</g>\n", |
| 81 | + "<!-- 1 -->\n", |
| 82 | + "<g id=\"node2\" class=\"node\">\n", |
| 83 | + "<title>1</title>\n", |
| 84 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n", |
| 85 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"99\" y=\"-156.95\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n", |
| 86 | + "</g>\n", |
| 87 | + "<!-- 0->1 -->\n", |
| 88 | + "<g id=\"edge1\" class=\"edge\">\n", |
| 89 | + "<title>0->1</title>\n", |
| 90 | + "<path fill=\"none\" stroke=\"black\" d=\"M126.65,-216.76C122.42,-208.55 117.19,-198.37 112.42,-189.09\"/>\n", |
| 91 | + "<polygon fill=\"black\" stroke=\"black\" points=\"115.68,-187.79 108,-180.49 109.46,-190.99 115.68,-187.79\"/>\n", |
| 92 | + "</g>\n", |
| 93 | + "<!-- 6 -->\n", |
| 94 | + "<g id=\"node7\" class=\"node\">\n", |
| 95 | + "<title>6</title>\n", |
| 96 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n", |
| 97 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"171\" y=\"-156.95\" font-family=\"Times,serif\" font-size=\"14.00\">8</text>\n", |
| 98 | + "</g>\n", |
| 99 | + "<!-- 0->6 -->\n", |
| 100 | + "<g id=\"edge6\" class=\"edge\">\n", |
| 101 | + "<title>0->6</title>\n", |
| 102 | + "<path fill=\"none\" stroke=\"black\" d=\"M143.35,-216.76C147.58,-208.55 152.81,-198.37 157.58,-189.09\"/>\n", |
| 103 | + "<polygon fill=\"black\" stroke=\"black\" points=\"160.54,-190.99 162,-180.49 154.32,-187.79 160.54,-190.99\"/>\n", |
| 104 | + "</g>\n", |
| 105 | + "<!-- 2 -->\n", |
| 106 | + "<g id=\"node3\" class=\"node\">\n", |
| 107 | + "<title>2</title>\n", |
| 108 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n", |
| 109 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"27\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">0</text>\n", |
| 110 | + "</g>\n", |
| 111 | + "<!-- 1->2 -->\n", |
| 112 | + "<g id=\"edge2\" class=\"edge\">\n", |
| 113 | + "<title>1->2</title>\n", |
| 114 | + "<path fill=\"none\" stroke=\"black\" d=\"M84.08,-146.5C74.23,-136.92 61.14,-124.19 49.97,-113.34\"/>\n", |
| 115 | + "<polygon fill=\"black\" stroke=\"black\" points=\"52.59,-111 42.98,-106.54 47.71,-116.02 52.59,-111\"/>\n", |
| 116 | + "</g>\n", |
| 117 | + "<!-- 3 -->\n", |
| 118 | + "<g id=\"node4\" class=\"node\">\n", |
| 119 | + "<title>3</title>\n", |
| 120 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n", |
| 121 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"99\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n", |
| 122 | + "</g>\n", |
| 123 | + "<!-- 1->3 -->\n", |
| 124 | + "<g id=\"edge3\" class=\"edge\">\n", |
| 125 | + "<title>1->3</title>\n", |
| 126 | + "<path fill=\"none\" stroke=\"black\" d=\"M99,-143.7C99,-136.41 99,-127.73 99,-119.54\"/>\n", |
| 127 | + "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-119.62 99,-109.62 95.5,-119.62 102.5,-119.62\"/>\n", |
| 128 | + "</g>\n", |
| 129 | + "<!-- 4 -->\n", |
| 130 | + "<g id=\"node5\" class=\"node\">\n", |
| 131 | + "<title>4</title>\n", |
| 132 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n", |
| 133 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"63\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">3</text>\n", |
| 134 | + "</g>\n", |
| 135 | + "<!-- 3->4 -->\n", |
| 136 | + "<g id=\"edge4\" class=\"edge\">\n", |
| 137 | + "<title>3->4</title>\n", |
| 138 | + "<path fill=\"none\" stroke=\"black\" d=\"M90.65,-72.76C86.42,-64.55 81.19,-54.37 76.42,-45.09\"/>\n", |
| 139 | + "<polygon fill=\"black\" stroke=\"black\" points=\"79.68,-43.79 72,-36.49 73.46,-46.99 79.68,-43.79\"/>\n", |
| 140 | + "</g>\n", |
| 141 | + "<!-- 5 -->\n", |
| 142 | + "<g id=\"node6\" class=\"node\">\n", |
| 143 | + "<title>5</title>\n", |
| 144 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"135\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n", |
| 145 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"135\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">5</text>\n", |
| 146 | + "</g>\n", |
| 147 | + "<!-- 3->5 -->\n", |
| 148 | + "<g id=\"edge5\" class=\"edge\">\n", |
| 149 | + "<title>3->5</title>\n", |
| 150 | + "<path fill=\"none\" stroke=\"black\" d=\"M107.35,-72.76C111.58,-64.55 116.81,-54.37 121.58,-45.09\"/>\n", |
| 151 | + "<polygon fill=\"black\" stroke=\"black\" points=\"124.54,-46.99 126,-36.49 118.32,-43.79 124.54,-46.99\"/>\n", |
| 152 | + "</g>\n", |
| 153 | + "<!-- 7 -->\n", |
| 154 | + "<g id=\"node8\" class=\"node\">\n", |
| 155 | + "<title>7</title>\n", |
| 156 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n", |
| 157 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"171\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">7</text>\n", |
| 158 | + "</g>\n", |
| 159 | + "<!-- 6->7 -->\n", |
| 160 | + "<g id=\"edge7\" class=\"edge\">\n", |
| 161 | + "<title>6->7</title>\n", |
| 162 | + "<path fill=\"none\" stroke=\"black\" d=\"M171,-143.7C171,-136.41 171,-127.73 171,-119.54\"/>\n", |
| 163 | + "<polygon fill=\"black\" stroke=\"black\" points=\"174.5,-119.62 171,-109.62 167.5,-119.62 174.5,-119.62\"/>\n", |
| 164 | + "</g>\n", |
| 165 | + "<!-- 8 -->\n", |
| 166 | + "<g id=\"node9\" class=\"node\">\n", |
| 167 | + "<title>8</title>\n", |
| 168 | + "<ellipse fill=\"none\" stroke=\"black\" cx=\"243\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n", |
| 169 | + "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"243\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">9</text>\n", |
| 170 | + "</g>\n", |
| 171 | + "<!-- 6->8 -->\n", |
| 172 | + "<g id=\"edge8\" class=\"edge\">\n", |
| 173 | + "<title>6->8</title>\n", |
| 174 | + "<path fill=\"none\" stroke=\"black\" d=\"M185.92,-146.5C195.77,-136.92 208.86,-124.19 220.03,-113.34\"/>\n", |
| 175 | + "<polygon fill=\"black\" stroke=\"black\" points=\"222.29,-116.02 227.02,-106.54 217.41,-111 222.29,-116.02\"/>\n", |
| 176 | + "</g>\n", |
| 177 | + "</g>\n", |
| 178 | + "</svg>\n" |
| 179 | + ], |
| 180 | + "text/plain": [ |
| 181 | + "TreeNode([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5])" |
| 182 | + ] |
| 183 | + }, |
| 184 | + "execution_count": 5, |
| 185 | + "metadata": {}, |
| 186 | + "output_type": "execute_result" |
| 187 | + } |
| 188 | + ], |
| 189 | + "source": [ |
| 190 | + "root" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": 4, |
46 | 196 | "id": "test", |
47 | 197 | "metadata": {}, |
48 | 198 | "outputs": [], |
|
65 | 215 | "file_extension": ".py", |
66 | 216 | "mimetype": "text/x-python", |
67 | 217 | "name": "python", |
68 | | - "nbconvert_exporter": "python3", |
| 218 | + "nbconvert_exporter": "python", |
69 | 219 | "pygments_lexer": "ipython3", |
70 | 220 | "version": "3.13.7" |
71 | 221 | } |
|
0 commit comments