二叉树的最近公共父节点

  给定一个二叉树和两个节点,求出这两个节点的最近公共父节点。如下图所示,节点8和节点13的最近公共父节点是3。注意,在此问题中,不存在一个节点是另一个节点的父节点的情况,如8和3的输入是无效的。

[Constraints]

The number of vertices are from 3 to 10000

[Input]

In each test case, the first line consists of four integers, V (the number of vertices in the tree), E (the number of edges), and the indices of two vertices. E edges are listed in the next line. Each edge is represented by two vertices; the index of the parent vertex always precedes the index of the child. For example, the edge connecting vertices 5 and 8 is represented by “5 8”, not by “8 5.” There is no order in which the edges are given. Every consecutive integer in the input is separated by a space. 

The indices of vertices are integers from 1 to V, and root vertex always has the index 1.

It is guaranteed that the parent vertex has smaller index than the child vertex.

In this problem, it is not important whether the child is the left child of the parent or the right child; so you can decide this arbitrarily.

[Output]

The answer has two integers: the index of the closest common ancestor and the size of the sub-tree rooted by the closest common ancestor. These two integers are separated by a space as well. 

比如输入

13 12 8 13

1 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 11 6 10 11 13

输出

3 8

或者输入:

100 99 24 7

38 9 68 100 81 10 1 62 25 24 15 92 73 75 16 87 84 86 59 72 94 67 37 70 69 46 88 35 62 90 28 15 5 61 43 98 10 89 21 88 76 69 87 47 80 23 49 31 63 19 61 99 54 78 18 36 79 33 11 30 77 40 47 26 73 95 22 91 15 38 50 44 62 39 100 13 93 97 89 53 39 37 14 80 5 54 49 94 48 58 55 12 40 82 51 3 87 7 45 4 82 14 31 32 30 64 93 42 10 48 40 25 21 77 37 71 90 50 35 76 20 34 96 59 25 85 77 81 58 56 57 79 76 11 81 84 14 57 60 16 88 8 96 21 43 63 83 17 57 93 94 18 69 28 28 5 42 2 98 55 70 74 80 66 91 73 61 68 13 41 35 51 1 96 19 29 79 43 60 83 68 27 51 65 42 52 11 45 70 60 90 22 39 49 52 20 17 6

输出:

1 100

实现思路:

1)求最近公共父节点。申请一个数组,数组的index对应二叉树的节点的序号,数组的值对应该index节点对应的父节点的序号。以

1 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 11 6 10 11 13

为例。

上表中,1为根节点,所以其父节点设置为-1,2的父节点为1,节点8的父节点为5,以此类推。

比如说要求8与13的最近公共父节点。

先求8到根节点1的路径。节点8的父节点是5,5的父节点是3,3的父节点是1,因此节点8到根节点的路径是8—5—3—1,

节点13到根节点的路径同理可得是13—11—6—3—1,

因此可以很容易知道8与13的最近公共父节点是两个节点到根节点路径的公共节点的最后一个节点3(从后往前看)。

2)求以某个节点为根节点的二叉树的所有节点数量。

保存每个节点的子节点数量,若没有子节点,则数量为0,若有一个左节点或者一个右节点,则数量为1(不考虑子节点的子节点数量),若左右子节点都存在,则数量为2。接下来就是统计从整个二叉树子节点数量之和,再加上本身数量1,就是该二叉树的节点总数。

如以3为根节点,则3有2个子节点,5、6各有2个子节点,8、9、10各有0个子节点,11有1个子节点,13有0个子节点,将这些节点数相加,再加上根节点3本身,就是以3位根节点的二叉树的节点数量。

代码如下:

#include <stdio.h>
#include <malloc.h>
#include <iostream>
struct node
{
    int parent;//父节点的索引
    int children[2];//存储左右孩子的索引
    int childrenNum;//孩子的数量,0或1或2
};

int caluNum(struct node *arr, int idx)
{
    if (idx == -1) return 0;    
    if (arr[idx].childrenNum == 1) 
        return 1 + caluNum(arr, arr[idx].children[0]);
    else if (arr[idx].childrenNum == 2) 
        return 2 + caluNum(arr, arr[idx].children[0])+ caluNum(arr, arr[idx].children[1]);
    else return 0;
}


int main()
{

    int nodeNum, edgeNum, node1, node2;
    scanf("%d %d %d %d", &nodeNum, &edgeNum, &node1, &node2);
    //std::cout << nodeNum << edgeNum << node1<< node2 <<std::endl;
    struct node* arr = (node*)malloc(sizeof(node)*(nodeNum + 1));//申请内存空间,用于存放所有节点
    for (int i = 0; i < nodeNum; i++)//初始化
    {
        arr[i].children[0] = -1;
        arr[i].children[1] = -1;
        arr[i].parent = -1;
        arr[i].childrenNum = 0;
    }
    for (int i = 0; i < edgeNum; i++)
    {
        int n1;
        scanf("%d", &n1);
        //std::cout << n << std::endl;
        int n2;
        scanf("%d", &n2);
        if (arr[n1].childrenNum == 0)
        {
            arr[n1].children[0] = n2;
            arr[n1].childrenNum = 1;
        }
        else
        {
            arr[n1].children[1] = n2;
            arr[n1].childrenNum = 2;
        }
        arr[n2].parent = n1;
    }
    //申请内存空间,用于存放从该节点一直到root节点的路径
    int* s1 = (int*)malloc(sizeof(int)*nodeNum);
    int num1 = 0;
    int* s2 = (int*)malloc(sizeof(int)*nodeNum);
    int num2 = 0;

    int n = node1;
    //存放从node1开始一直到root节点的路径
    while (n!=-1)
    {
        s1[num1++] = n;
        n = arr[n].parent;
    }
    //for (int i = 0; i < num1; i++)
    //{
    //    std::cout << s1[i] << std::endl;
    //}
    n = node2;
    while (n != -1)
    {
        s2[num2++] = n;
        n = arr[n].parent;
    }
    //for (int i = 0; i < num2; i++)
    //{
    //    std::cout << s2[i] << std::endl;
    //}


    int commonAncestor = 0;
    num1--;
    num2--;
    while (s1[num1--] == s2[num2--])
    {
        commonAncestor = s1[num1+1];//获取最近公共父节点
    }
    //输入最近公共父节点
    std::cout << commonAncestor << std::endl;

    //计算以该节点为root节点的二叉树的节点总数
    int totalNum = caluNum(arr, commonAncestor) + 1;


    std::cout << totalNum << std::endl;
    free(arr);

    return 0;
}

 附:部分测试样例

13 12 8 13
1 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 11 6 10 11 13
10 9 2 10
1 2 1 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
50 49 24 31
31 7 2 17 27 32 14 30 1 21 45 26 44 27 39 11 26 3 48 6 3 44 2 49 42 13 48 8 23 33 11 10 8 42 41 31 17 4 8 22 25 23 21 41 28 25 13 16 46 2 31 35 42 19 32 18 27 50 45 15 28 20 46 28 44 40 40 5 15 48 9 34 1 46 17 29 35 36 21 45 14 37 23 14 6 39 11 9 19 24 26 47 16 38 40 12 47 43
100 99 24 7
38 9 68 100 81 10 1 62 25 24 15 92 73 75 16 87 84 86 59 72 94 67 37 70 69 46 88 35 62 90 28 15 5 61 43 98 10 89 21 88 76 69 87 47 80 23 49 31 63 19 61 99 54 78 18 36 79 33 11 30 77 40 47 26 73 95 22 91 15 38 50 44 62 39 100 13 93 97 89 53 39 37 14 80 5 54 49 94 48 58 55 12 40 82 51 3 87 7 45 4 82 14 31 32 30 64 93 42 10 48 40 25 21 77 37 71 90 50 35 76 20 34 96 59 25 85 77 81 58 56 57 79 76 11 81 84 14 57 60 16 88 8 96 21 43 63 83 17 57 93 94 18 69 28 28 5 42 2 98 55 70 74 80 66 91 73 61 68 13 41 35 51 1 96 19 29 79 43 60 83 68 27 51 65 42 52 11 45 70 60 90 22 39 49 52 20 17 6
200 199 45 145
181 101 35 149 49 95 134 16 69 35 127 59 28 92 168 51 81 131 2 20 61 161 40 113 71 63 67 42 21 198 193 108 17 13 194 103 76 11 101 73 39 30 186 69 91 96 174 133 39 38 136 74 142 187 164 178 77 200 128 141 110 118 14 56 1 194 60 166 148 173 51 167 124 39 180 181 47 128 18 22 52 81 72 90 26 123 53 66 78 146 155 156 79 158 171 12 63 134 192 57 147 195 47 65 99 62 65 169 77 154 165 130 57 175 188 115 190 93 10 165 59 4 98 139 194 86 197 114 83 148 175 160 140 99 117 193 165 25 167 67 177 183 123 188 13 23 186 60 166 54 12 176 163 168 188 32 195 41 107 104 42 75 85 126 112 132 84 135 7 129 65 64 8 2 119 46 44 10 50 152 184 153 53 116 17 172 92 144 58 150 172 3 162 184 50 182 97 109 49 87 190 36 41 34 80 179 103 106 84 145 76 21 71 94 23 138 58 140 166 78 28 88 99 33 73 5 117 112 167 177 136 40 192 28 109 27 152 31 174 117 172 98 38 84 63 43 156 15 6 79 54 47 171 136 73 6 124 17 94 61 197 157 45 191 45 29 103 58 110 9 147 155 97 124 140 171 134 125 81 159 92 142 15 127 36 8 168 119 11 199 115 44 91 111 105 89 26 180 22 71 176 102 42 77 106 80 106 164 29 137 60 37 32 7 170 105 54 121 38 52 120 122 152 19 114 174 22 170 170 76 150 197 87 82 115 45 86 147 162 68 19 48 155 185 180 70 3 151 163 186 13 107 86 143 85 120 3 83 69 192 123 91 18 26 118 196 143 189 51 18 67 49 41 190 1 163 79 14 111 72 193 55 27 162 150 50 5 53 109 85 78 24 119 97 195 100 133 110
500 499 186 167
15 358 87 369 34 247 367 162 61 282 498 74 207 315 241 439 296 221 134 124 234 243 181 23 477 266 239 213 490 174 495 268 21 184 253 478 423 224 423 276 174 218 204 424 242 352 222 380 410 6 39 71 3 441 259 346 353 494 331 93 132 284 168 490 93 300 346 180 411 225 374 48 1 126 127 397 435 117 183 3 317 476 471 269 63 280 464 52 491 169 121 85 484 274 110 14 344 4 164 408 292 142 259 69 427 455 266 175 226 79 157 86 166 98 326 281 430 366 214 118 382 491 315 308 92 294 471 250 45 325 4 433 168 42 444 391 182 458 389 212 125 415 126 230 495 67 271 350 142 210 425 389 469 461 399 425 254 57 161 47 306 7 329 167 360 21 290 206 86 88 15 185 433 151 26 138 185 396 437 80 364 401 112 309 44 99 220 381 477 447 373 480 456 139 225 403 170 492 25 160 251 220 224 228 58 290 485 386 156 217 467 61 135 158 6 114 77 146 482 319 178 135 147 262 200 32 42 444 444 203 305 232 198 152 339 208 464 304 52 55 114 435 296 402 177 31 272 26 58 422 337 487 144 413 30 314 249 171 469 35 403 467 36 41 427 436 213 120 229 190 321 297 23 25 111 302 130 195 326 482 439 211 144 368 30 407 462 104 400 286 190 193 297 400 202 103 260 328 171 240 400 90 282 150 120 173 197 186 208 477 166 363 377 469 174 2 270 374 327 39 428 409 439 465 496 75 252 192 249 134 11 231 323 264 460 154 24 361 52 419 12 392 445 19 50 449 172 278 349 76 13 234 1 168 265 481 284 72 235 303 470 453 340 95 161 337 113 443 244 411 496 310 280 165 479 191 181 464 264 457 412 466 48 426 229 271 272 295 465 209 110 28 442 46 286 60 306 127 170 148 409 326 34 176 455 365 397 434 362 254 263 279 499 242 354 260 225 121 197 289 121 236 428 178 28 197 487 448 135 219 461 51 292 194 254 24 490 11 436 115 51 296 14 291 28 427 300 406 91 258 231 417 463 179 75 161 391 49 304 470 484 59 194 474 304 13 404 393 27 110 406 497 136 64 435 65 322 335 315 233 42 410 103 22 92 205 152 376 239 404 215 301 289 36 382 223 59 334 5 323 246 248 27 200 126 353 158 307 252 398 277 416 251 81 222 12 99 183 263 348 470 70 407 226 213 251 152 15 355 356 56 267 440 189 164 202 397 20 199 164 338 364 295 342 432 418 388 316 147 235 494 84 171 311 317 215 80 312 421 252 452 107 369 5 215 214 422 324 417 249 62 241 199 170 425 237 23 156 154 58 432 244 189 145 64 137 211 382 288 16 157 259 264 438 55 92 357 56 119 270 9 38 8 163 230 157 84 399 218 283 82 116 476 333 421 320 194 45 66 429 182 451 200 372 74 338 377 321 183 123 373 468 371 485 201 265 344 379 94 341 87 166 160 128 234 459 5 159 284 256 202 298 51 317 279 199 238 388 106 496 20 285 19 378 291 37 323 97 403 495 180 66 198 370 138 332 360 339 437 77 277 109 9 187 184 122 455 367 479 246 61 347 84 10 33 345 332 153 418 9 467 8 376 253 82 96 274 261 299 421 240 423 102 245 454 113 190 292 231 62 46 385 410 377 203 140 217 359 80 277 3 394 305 384 142 125 244 354 177 18 76 89 451 50 211 437 454 149 2 460 207 29 413 371 2 340 13 462 404 472 160 486 339 257 253 499 419 188 412 172 209 238 55 33 241 44 291 94 29 431 154 360 25 63 474 318 188 322 119 106 125 420 91 330 302 216 62 343 475 484 206 272 418 440 85 430 4 30 395 473 106 493 451 91 331 87 329 275 299 471 461 428 486 201 324 414 498 101 209 102 143 336 232 375 39 488 44 112 494 475 302 196 208 344 422 222 108 390 369 144 460 27 376 313 6 331 327 207 7 111 93 349 53 155 149 287 411 141 493 479 349 357 22 452 340 147 466 136 260 500 491 355 321 498 221 454 285 405 417 34 188 182 387 329 32 131 314 483 65 54 485 177 114 130 295 105 433 288 21 229 341 273 275 129 443 446 370 306 111 17 230 181 290 383 14 119 45 305 86 442 465 263 218 293 108 412 436 450 127 299 156 108 107 445 103 133 475 83 46 53 297 82 441 43 358 68 137 73 189 387 338 373 257 489 419 204 289 78 7 456 348 100 406 40 353 198 88 362 242 132 370 432 413 327 204 395 88 351 357 227 101 463 362 255 271 143 11 239

测试结果:

#1 3 8
#2 1 10
#3 21 35
#4 1 100
#5 168 107
#6 1 500
原文地址:https://www.cnblogs.com/hejunlin1992/p/7774212.html