求 一棵二叉树中权值最大和最小的叶节点之间的距离

#include <iostream>
#include <vector>

//结点的数据结构
struct Node
{
	int _data;
	int _weight;
	Node* _left;
	Node* _right;
	Node(const int& x = 0,int weight=0)
		:_left(NULL)
		, _right(NULL)
		, _data(x)
		, _weight(weight)
	{}
};

//建树
Node* CreateTree(const int* array,const int* weight,size_t size,int& i)//i=0
{
	/*if (array == NULL || weight==NULL || size == 0)
		return NULL;*/
	if (array[i] == ‘#‘)
		return NULL;
	Node* root = new Node(array[i],weight[i]);
	root->_left = CreateTree(array,weight,size,++i);
	root->_right = CreateTree(array, weight, size, ++i);
	return root;
}

//先序遍历,把节点放到容器v中,结点对应的深度放到vdepth中(根结点的深度为0)
Node* pushData(Node* root, int& max, int& min, std::vector<Node*>& v, 	std::vector<int>& vdepth,int depth)
{
	Node* cur = root;
	if (cur){
		v.push_back(cur);
		vdepth.push_back(depth);
	}
	while (cur){
		pushData(cur->_left, max, min, v, vdepth, ++depth);
		if (cur->_left != NULL){
			v.push_back(cur);
			vdepth.push_back(--depth);
		}
		else
			--depth;
		pushData(cur->_right, max, min, v, vdepth, ++depth);
		if (cur->_right != NULL){
			v.push_back(cur);
			vdepth.push_back(--depth);
		}
		else{
			--depth;
			if (cur->_weight > max)
				max = cur->_weight;
			if (cur->_weight < min)
				min = cur->_weight;
		}
		return cur;
	}
	return NULL;
}

int bigAndsmallWeightDistance(Node* root, int& max, int& min, std::vector<Node*>& v, 	std::vector<int>& vdepth, int depth)
{
	pushData(root, max, min, v, vdepth, depth);
	int maxindex, minindex;
	//找到权值最大和最小的叶结点在v中对应的下标
	for (int i = 0; i < v.size(); ++i){
		if (v[i]->_weight == max){
			maxindex = i;
		}
		if (v[i]->_weight == min){
			minindex = i;
		}
	}
	int i, j;
	if (maxindex > minindex){
		i = minindex;
		j = maxindex;
	}
	int retminindex = i;
	for (i; i <= j; ++i){
		if (vdepth[i] < retminindex){
			retminindex = vdepth[i];//retminindex下标所对应的v中结点即为所找叶子结点的公共父节点
		}
	}
	//至此,就找到了这两个权值最大和最小的叶节点和它们的公共祖先节点的下标

	return vdepth[maxindex] + vdepth[minindex] - 2 * vdepth[retminindex];
}

int main()
{
	/*int array[] = { 1, 2, 3, ‘#‘, ‘#‘, 4, 5, ‘#‘, ‘#‘, 6, ‘#‘, ‘#‘, 7, ‘#‘, 8, ‘#‘, ‘#‘ };
	int weight[] = { 4, 2, 1, ‘#‘, ‘#‘, 5, 7, ‘#‘, ‘#‘, 8, ‘#‘, ‘#‘, 6, ‘#‘,  3, ‘#‘, ‘#‘, };*/
	int array[] = { 1, 2, 3, 11, ‘#‘, 12, ‘#‘, ‘#‘, ‘#‘, 4, 5, ‘#‘, ‘#‘, 6, 9, ‘#‘, 10, ‘#‘, ‘#‘, ‘#‘, 7, ‘#‘, 8, ‘#‘, ‘#‘ };
	int weight[] = { 4, 2, 2, 13, ‘#‘, 1, ‘#‘, ‘#‘, ‘#‘, 5, 7, ‘#‘, ‘#‘, 8, 9, ‘#‘, 10, ‘#‘, ‘#‘, ‘#‘,6, ‘#‘, 3, ‘#‘, ‘#‘, };
	int i = 0;
	Node* root = CreateTree(array,weight,sizeof(array)/sizeof(array[0]),i);
	std::vector<Node*> v;
	std::vector<int> vdepth;
	int depth = 0;
	int maxweight=-1;
	int minweight = 100;
	int distance = bigAndsmallWeightDistance(root, maxweight, minweight, v, vdepth, depth);
	std::cout << distance << std::endl;

	system("pause");
}
时间: 2024-08-14 09:32:52

求 一棵二叉树中权值最大和最小的叶节点之间的距离的相关文章

二叉树进阶之求一棵二叉树中结点间最大距离

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6618074.html 二叉树中的结点间距离:从结点A出发到达B,每个结点只能走一次,AB路径上的结点数就是AB间距离. 由于从一个结点出发时,只有两种方向可走:向上经过父节点到达它的兄弟子树:向下到达它自己的左右子树: 对于一个结点h为根的子树:假设现在从h左子树中最深的叶结点逐层向上走,一直走到h的左儿子,现在h.left有两种选择: 一是向上经过h,然后到达h的右子树向下走到最深叶结点: 二是从h.le

笔试算法题(36):寻找一棵二叉树中最远节点的距离 &amp; 根据二叉树的前序和后序遍历重建二叉树

出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所以计算出以每个节点为根节点的子树的最 远距离,最后取他们的最大值就是整棵树的最远距离: 如果递归层次过多造成系统栈溢出,则可以使用stack堆栈结构存储递归节点,从而使用循环实现 解题: 1 struct Node { 2 int value; 3 Node *left; 4 Node *right

019写程序在一棵二叉树中找到两个结点的最近共同祖先(keep it up)

写程序在一棵二叉树中找到两个结点的最近共同祖先. 分两种情况来讨论这个题: 第一种情况结点中没有指向父结点的指针 第二种情况接种有指向父节点的指针 我们先看第一种情况,结点中没有指向父结点的指针. 我们可以采用暴力搜索每一个结点,如果这个结点的子树中 有已知的两个结点,那我们就继续沿着左右子树找,如果左子树 能找到,我们就继续沿着左子树找,如果有子树能找到,我们就 沿着右子树找,不存在两个子树都能够找到. 代码: struct TreeNode {<pre name="code"

编程实现求一棵二叉树的最短树路径和最长树路径

Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. class Solution { public: int minDepth(TreeNode *root) { if(!r

hdu-4738.Caocao&#39;s Bridges(图中权值最小的桥)

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10933    Accepted Submission(s): 3065 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi.

二叉树(9)----打印二叉树中第K层的第M个节点,非递归算法

1.二叉树定义: typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.求二叉树中第K层的第M个节点 (1)非递归算法 借助队列实现 首

POJ 题目2750 Potted Flower(线段树求环型区间中连续区间的最大和)

Potted Flower Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4470   Accepted: 1698 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of f

二叉树进阶之寻找一棵二叉树中的最大二叉搜索子树

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6618915.html  (规律:在二叉树中寻找某性质的,都应该以递归思维:从根结点开始递归左右,一直到底,由底向上返回的信息来判断当前结点.求当前结点.即:二叉树的题目,从下往上想,递归的返回过程就是从下往上由叶到根建立二叉树的过程,在此过程中对每一步的"根"结点作性质判断,返回到根时即是整棵树的性质判断了) 从一棵树中寻找结点数最多的二叉搜索子树,并返回这棵子树的头结点. 从题目我们知道以下要求

caffe中权值初始化方法

首先说明:在caffe/include/caffe中的 filer.hpp文件中有它的源文件,如果想看,可以看看哦,反正我是不想看,代码细节吧,现在不想知道太多,有个宏观的idea就可以啦,如果想看代码的具体的话,可以看:http://blog.csdn.net/xizero00/article/details/50921692,写的还是很不错的(不过有的地方的备注不对,不知道改过来了没). 文件 filler.hpp提供了7种权值初始化的方法,分别为:常量初始化(constant).高斯分布初