2

Gooooogle  •  4个月前


#include <bits/stdc++.h>
using namespace std;
int sum = 0;

typedef struct node {
	int data;
	node *l;
	node *r;
} *tree;

void week(tree &root) {
	int x;
	cin >> x;
	if (x == -1) {
		return;
	} else {
		root = new node();
		root->l = NULL;
		root->r = NULL;
		root->data = x;
	}
	week(root->l);
	week(root->r);
}

void kend(tree &root, int s) {
	if (root != NULL) {

		if (root->l == NULL && root->r == NULL && s == 1) {
			sum += root->data;
		}
		kend(root->l, 1);
		kend(root->r, 0);
	}
}

int main() {
	tree T;
	week (T);
	kend(T, 0);
	cout << sum;

	return 0;
}

评论:

请先登录,才能进行评论