求二叉树中节点的最大距离

编程之美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));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值