求二叉树的高度,叶子节点个数,第K层结点个数,求祖先结点问题

一、求二叉树的高度

类似归并排序的思想。先求最底层结点的高度,再分别比较生成更高的结点的高度。最后递归至根结点,求出根结点的高度。

//求二叉树的高度
	int Height()
	{
		return GetHeight(_root);
	}
	int GetHeight(Node<T> *root)
	{
		int left = 0;
		int right = 0;
		if (root->_leftchild != NULL)
			left += GetHeight(root->_leftchild);
		if (root->_rightchild != NULL)
			right += GetHeight(root->_rightchild);
		return left >= right ? (left + 1) : (right + 1);
	}

二、求叶子结点的个数

分别递归左右子树,当最后出现结点左右孩子均为空时,其为叶结点,从而进行加一操作。

//求叶子节点的个数
	int count_leaves()
	{
		return count_leaves(_root);
	}
	int count_leaves(Node<T> *root)
	{
		int count = 0;
		if (root == nullptr)
			return count;
		if (root->_leftchild != nullptr)
		{
			count += count_leaves(root->_leftchild);
		}
		if (root->_rightchild != nullptr)
		{
			count += count_leaves(root->_rightchild);
		}
		if (root->_leftchild == nullptr && root->_rightchild == nullptr)
			count += 1;
		return count;
	}

三、求第K层结点的个数

根据思想,求第k层的结点个数,即第k-1层结点的子结点的个数,而第k-1层结点又可以由第k-2层求出.......

	//计算二叉树第K层结点的个数
	int count_Node(int k)
	{
		return count_Node(_root, k);
	}
	int count_Node(Node<T> *root, int k)
	{
		int count = 0;
		if (k == 1)
		if (root != NULL)
			return count += 1;
		if (k > 1)
		{
			if (root->_leftchild != nullptr)
				count += count_Node(root->_leftchild, k - 1);
			if (root->_rightchild != nullptr)
				count += count_Node(root->_rightchild, k - 1);
		}
		return count;
	}

四、求最近的祖先结点

最近的祖先结点:即可以到达两个结点,并且离两个结点最近的那个结点。可以编写一个判断结点是否存在二叉树的find函数,从而通过从根结点开始遍历其左右子树,一直到无法同时走到那两个结点的结点位置(递归最深的那次),则上一个结点即为最近的祖先结点。

//求最近公共祖先结点
	Node<T>* findancestor(T x1, T x2)
	{
		return findancestor(_root, x1, x2);
	}
	Node<T>* findancestor(Node<T> *root, T x1, T x2)
	{
		if (root == nullptr)
			return nullptr;
		if (findNode(root, x1) && findNode(root, x2))
		{
			if (findancestor(root->_leftchild, x1, x2) != nullptr)
				root = root->_leftchild;
			if (findancestor(root->_rightchild, x1, x2) != nullptr)
				root = root->_rightchild;
			return root;
		}
		return nullptr;
	}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 15:11:47

求二叉树的高度,叶子节点个数,第K层结点个数,求祖先结点问题的相关文章

二叉树的层序遍历、二叉树叶节点输出算法、求二叉树的高度、层序创建一棵二叉树

二叉树的层序遍历 1 void LevelorderTraversal(BinTree BT) 2 { 3 std::queue<BinTree> Queue; 4 BinTree T; 5 if (!BT) 6 return; //若是空树则直接返回 7 Queue.push(BT); 8 while (!Queue.empty()) 9 { 10 T = Queue.front(); 11 Queue.pop(); 12 printf("%c ", T->Data

打印二叉树两个叶子节点间的路径

简要代码 /* * File : print binary tree road between leaf A and leaf B * Date : 2015/8/2 * Author : jinya * Assert A --> B * traverse root , left , right ; left , root , right ; left , right , root; * so left is always before right * 左边叶子节点遍历总是优先于右边叶子节点 *

求二叉树中两个节点的最远距离

问题定义 如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数.写一个程序求一棵二叉树中相距最远的两个节点之间的距离. 计算一个二叉树的最大距离有两个情况: 情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点. 情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者. 思路: 1,后序遍历每一节点,找出该节点到最右边的距离以及最左边的距离: 2,找到之和最大的即可. //需保存左子树中最长距离.右子树最长

求二叉树中两个节点的最低公共父节点

必须通过遍历查找一个节点的祖先集合,然后比较两个节点的祖先集合就可以找到最低的那个.这里采用后序遍历,并传入一个栈记录该节点的祖先节点.在每次访问一个节点时,先把这个节点压入栈,然后判断该节点是不是要查找的那个节点,如果是返回.接着查找它的左子树和右子树,当要查找的节点在它的左右子树中则返回.然后判断该节点与栈顶节点是否相同,是则弹出栈顶元素.这是因为相同就代表了在访问它的左右子树时没有添加新的节点,也就是说要查找的那个节点不在它的左右子树中,则该节点也就是不是要查找的节点的祖先. #inclu

数据结构与算法-第12章二叉树和其他树-003求二叉树的高度

The code to find the tree height using a postorder traversal is given below. 1 public class BinaryTreeHeight 2 { 3 /** @return tree height */ 4 public static int height(BinaryTreeNode t) 5 { 6 if (t != null) 7 {// nonempty tree 8 // find height of le

根据二叉树的先序遍历和中序遍历还原二叉树并且求二叉树的高度

#include<iostream> #include<string> using namespace std; string s1, s2; class Tree { public: char c; Tree *left; Tree *right; }; Tree* create() { Tree *p = new Tree; p->left = p->right = NULL; return p; } Tree* huanyuan(int x1, int x2, i

求二叉树的高度(非递归)

非递归就是在层次遍历的基础上加上个depth,len变量来记录即可,有点类似于BFS 用c++实现如下: 1 int TreeDepth(TreeNode* pRoot) 2 { 3 queue<TreeNode*> q; 4 if(!pRoot) return 0; 5 q.push(pRoot); 6 int depth=0; 7 while(!q.empty()){ 8 int len=q.size();//队列的当前大小 9 depth++; 10 while(len--){ //循环

构造二叉树,并求解树的高度

一,问题描述 在控制台上输入一组数据,请按照输入的数据的格式来构造一棵二叉树,并打印出二叉树的高度. 输入的数据格式如下: 第一行为一个整数N(其实是二叉树中边的数目),表示接下来一共有N行输入,每行输入有两个数,左边的数表示父结点,右边的数表示父结点的孩子结点.示例如下: 6 0 1 0 2 1 3 2 4 2 5 4 6 从上面的输入可以看出:①根结点0 的左孩子为1,右孩子为2 .②结点1 只有一个孩子,即左孩子3 二,问题分析 问题的关键是根据上面的输入数据 构造一棵二叉树. 首先用一个

分别求二叉树前、中、后序的第k个节点

一.求二叉树的前序遍历中的第k个节点 //求先序遍历中的第k个节点的值 int n=1; elemType preNode(BTNode *root,int k){ if(root==NULL) return ' '; if(n==k) return root->data; n++; elemType ch = preNode(root->lchild,k); if(ch!=' ') return ch; ch = preNode(root->rchild,k); return ch;