50110. Tree Operations

難度:3/5

記得看清楚題目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "operations.h"

// typedef struct Node{
// int n;
// struct Node *left, *right;
// } Node;

Node *FlipTree(Node *root){
if(!root) return NULL;

Node* new_root = (Node*) malloc(sizeof(Node));
new_root->n = root->n;
new_root->left = FlipTree(root->right);
new_root->right = FlipTree(root->left);
return new_root;
}

int isIdentical(Node *root1, Node *root2){
if(root1 == NULL && root2 == NULL) return 1;
else if(root1 == NULL || root2 == NULL) return 0;
else{
return (root1->n == root2->n) && isIdentical(root1->left, root2->left)
&& isIdentical(root1->right, root2->right);
}
}

int sum = 0;
int num = 0;

void dfs(Node *root, int d){
if(root->left == NULL && root->right == NULL){
sum += d;
num++;
}

if(root->left) dfs(root->left, d + 1);
if(root->right) dfs(root->right, d + 1);
}

void depthAvg(Node *root){
sum = 0;
num = 0;
dfs(root, 0);
printf("%d/%d\n", sum, num);
}


50110. Tree Operations
https://aaronlin1229.github.io/judgegirl_50110/
Author
Akizumi
Posted on
July 17, 2023
Licensed under