编程之美3.8
#include <iostream>
#include <queue>
using namespace std;
struct Node
{
Node(int i = 0, Node *pLeft = NULL, Node *pRight = NULL) : data(i), left(pLeft), right(pRight) {}
int data;
Node *left;
Node *right;
int maxDist;
};
Node *construct()
{
Node *node10 = new Node(10);
Node *node9 = new Node(9, node10, NULL);
Node *node8 = new Node(8);
Node *node7 = new Node(7, NULL, node9);
Node *node6 = new Node(6);
Node *node5 = new Node(5, node8);
Node *node4 = new Node(4);
Node *node3 = new Node(3, node6, node7);
Node *node2 = new Node(2, node4, node5);
Node *node1 = new Node(1, node2, node3);
return node1;
}
int maxDistance(Node *root, int &dist)
{
if (root == NULL)
return -1;
if (root->left == NULL && root->right == NULL)
{
root->maxDist = 0;
return 0;
}
int leftDepth = maxDistance(root->left, dist);
int rightDepth = maxDistance(root->right, dist);
int leftMaxDist;
int rightMaxDist;
if (root->left)
leftMaxDist = root->left->maxDist;
else
leftMaxDist = 0;
if (root->right)
rightMaxDist = root->right->maxDist;
else
rightMaxDist = 0;
dist = max(max(leftMaxDist, rightMaxDist), leftDepth + rightDepth + 2);
root->maxDist = dist;
int depth = max(leftDepth, rightDepth) + 1;
return depth;
}
void main()
{
Node *root = construct();
int dist = 0;
maxDistance(root, dist);
cout << "maxDist: " << dist << endl;
}
迭代求解最大值
#include <iostream>
#include <stack>
using namespace std;
struct Node
{
Node(int i = 0, Node *pLeft = NULL, Node *pRight = NULL) : data(i), left(pLeft), right(pRight) {}
int data;
Node *left;
Node *right;
int depth;
int dist;
};
Node *construct()
{
Node *node10 = new Node(10);
Node *node9 = new Node(9, node10, NULL);
Node *node8 = new Node(8);
Node *node7 = new Node(7, NULL, node9);
Node *node6 = new Node(6);
Node *node5 = new Node(5, node8);
Node *node4 = new Node(4);
Node *node3 = new Node(3, node6, node7);
Node *node2 = new Node(2, node4, node5);
Node *node1 = new Node(1, node2, node3);
return node1;
}
int findMaxDist(Node *root)
{
if (root == NULL)
return -1;
Node *pRoot = root;
stack<Node*> nstack;
nstack.push(root);
Node *prev = NULL;
while (!nstack.empty())
{
root = nstack.top();
if (root->left != prev && root->right != prev)
{
if (root->right != NULL)
nstack.push(root->right);
if (root->left != NULL)
nstack.push(root->left);
}
if (root->left == NULL && root->right == NULL)
{
root->depth = 0;
root->dist = 0;
nstack.pop();
}
if (root->left == prev || root->right == prev)
{
int leftDist = root->left == NULL ? 0 : root->left->dist;
int rightDist = root->right == NULL ? 0 : root->right->dist;
int leftDepth = root->left == NULL ? 0 : root->left->depth;
int rightDepth = root->right == NULL ? 0 : root->right->depth;
root->depth = max(leftDepth, rightDepth) + 1;
root->dist = max(leftDist, max(rightDist, leftDepth + rightDepth + 2));
nstack.pop();
}
prev = root;
}
return pRoot->dist;
}
void main()
{
Node *root = construct();
int dist = findMaxDist(root);
cout << "dist = " << dist << endl;
}
更优秀的递归程序
int findMaxDist(Node *root, int &depth)
{
if (root == NULL)
{
depth = -1;
return 0;
}
int leftDepth;
int rightDepth;
int leftDist = findMaxDist(root->left, leftDepth);
int rightDist = findMaxDist(root->right, rightDepth);
depth = max(leftDepth, rightDepth) + 1;
return max(leftDist, max(rightDist, leftDepth + rightDepth + 2));
}