1.个结点的最近公共祖先
实现思路:保存两个节点的路径,然后进行判断相同根节点
typedef BNode TreeNode;
bool GetNodePath(TreeNode* pHead, TreeNode* pNode, std::list &list)
{
//没有找到,就在就直接pop
if (pHead == pNode)
{
return true;
}
list.push_back(pHead);
bool found = false;
//前序遍历
if (pHead->pLeft)
{
found = GetNodePath(pHead->pLeft, pNode, list);
}
if (!found&&pHead->pRight)
{
found = GetNodePath(pHead->pRight, pNode, list);
}
if (!found)
{
list.pop_back();
}
return found;
}
//求最近公共祖先
TreeNode* GetLastCommon
(
const std::list &path1,
const std::list &path2
)
{
std::list ::const_iterator it1 = path1.begin();
std::list ::const_iterator it2 = path2.begin();
TreeNode* pLast = NULL;
while (it1 != path1.end() && it2 != path2.end())
{
if (*it1 == *it2)
{
pLast = *it1;
}
else
{
break;
}
it1++;
it2++;
}
return pLast;
}
TreeNode* LastCommonParent(TreeNode*pHead, TreeNode* pNode1, TreeNode* pNode2)
{
if (pHead == NULL || pNode1 == NULL || pNode2 == NULL)
{
return NULL;
}
//获取两条路径
std::list path1;
GetNodePath(pHead,pNode1,path1);
std::list path2;
GetNodePath(pHead, pNode2, path2);
return GetLastCommon(path1,path2);
}
2.最远两个结点的距离
实现思路:两个结点的最远距离,就是左右子树的高度之和,求最大值
从下往上求,后序遍历
size_t GetFarthestDistance(TreeNode* pHead,size_t&maxLength)
{
if (pHead == NULL)
{
return 0;
}
size_t left=GetFarthestDistance(pHead->pLeft,maxLength);
size_t right=GetFarthestDistance(pHead->pRight,maxLength);
if (left + right > maxLength)
{
maxLength = left + right;
}
return left > right ? left + 1 : right + 1;
}
3.叶子节点的个数
思路:遍历一次树,当左右孩子均为空,说明就是叶子节点,进行计数
void _GetLeafCount(BNode* pRoot,size_t&count)
{
if (pRoot)
{
if (!(pRoot->pLeft) && !(pRoot->pRight))
{
count++;
}
_GetLeafCount(pRoot->pLeft, count);
_GetLeafCount(pRoot->pRight, count);
}
}
4.第K 层节点的个数
void _GetKthLeafCount(BNode* pRoot,size_t k,size_t K, size_t&count)
{
if (pRoot)
{
if (k==K)
{
count++;
}
k++;
_GetKthLeafCount(pRoot->pLeft,k,K ,count); //注意此处是个坑,一次递归中不能++ 两次
_GetKthLeafCount(pRoot->pRight,k,K, count);
}
}
5.一个数组中有一个数字的次数超过了数组的一半,求出这个字符。如:int a[]={2,3,2,2,2,2,2,5,4,1,2,3},求出超过一半的数字是2。
//思路1:用一个计数器,一个保存值,如果相等,则进行++,不相等进行–,等于-1时,进行更新改值
//思路2:用快速排序排序,中间元素即是该元素
size_t GetHalfTimesNum1(int* arr,size_t size)
{
if (arr == NULL || size < 1)
{
return 0;
}
int num = arr[0];
int count = 0;
for (int i = 0; i < size; i++)
{
if (num == arr[i])
{
count++;
}
else
{
count--;
}
if (count < 0)
{
num = arr[i];
}
}
return num;
}
size_t GetHalfTimesNum2(int* arr, size_t size)
{
sort(arr, arr + size);
return arr[size/2];
}