在本教程中,我们将讨论一个在二叉树中查找最大父子和的程序。
为此,我们将提供一个二叉树。我们的任务是将父节点及其子节点相加,最后找出所有节点的最大值并打印出来。
#include <iostream> using namespace std; struct Node { int data; struct Node *left, *right; }; //插入节点 struct Node* newNode(int n) { struct Node* root = new Node(); root->data = n; root->left = root->right = NULL; return root; } int maxSum(struct Node* root) { if (root == NULL) return 0; int res = maxSum(root->left); if (root->left != NULL && root->right != NULL) { int sum = root->data + root->left->data + root->right->data; res = max(res, sum); } return max(res, maxSum(root->right)); } int main() { struct Node* root = newNode(15); root->left = newNode(16); root->left->left = newNode(8); root->left->left->left = newNode(55); root->left->right = newNode(67); root->left->right->left = newNode(44); root->right = newNode(17); root->right->left = newNode(7); root->right->left->right = newNode(11); root->right->right = newNode(41); cout << maxSum(root); return 0; }
输出结果
91