【每日一题-11】求二叉树高度/销毁一棵二叉树与链表翻转

求二叉树的高度

int TreeDepth(Node* root)
{
	if (root == NULL)
		return 0;
	if (root->left == NULL&&root->right == NULL)
		return 1;
	int leftLen = TreeDepth(root->left);
	int rightLen = TreeDepth(root->right);
	return (leftLen > rightLen) ? (leftLen + 1) : (rightLen + 1);
}
销毁一颗二叉树-->Destroy(Node* root)

void _Destory(Node* root)
{
	if (root == NULL)
		return;

	_Destory(root->_left);
	_Destory(root->_right);
	delete root;
}
链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现Node* RotateList(Node* list, size_t k). 提示:这个题是链表逆置的升级变型。

#include<iostream>
#include<vector>
using namespace std;

typedef struct Node
{
	int val;
	struct Node* next;
}Node;

Node* ReverseList(Node* pHead)
{
	if (NULL == pHead || pHead->next == NULL)
		return NULL;
	Node *cur;
	Node *pNew = NULL;
	while (pHead)
	{
		cur = pHead;
		pHead = pHead->next;
		cur->next = pNew;
		pNew = cur;
	}
	return pNew;
}

Node* getLastNode(Node* pHead)
{
	while (NULL != pHead->next)
	{
		pHead = pHead->next;
	}
	return pHead;
}

Node* swapListByk(Node* pHead, int k)
{
	if (k <= 1)
	{
		return pHead;
	}
	int pos;
	Node* pNode = pHead;
	Node* newHead;
	Node* pNextNode;
	Node* pLastNode = NULL;
	pHead = NULL;
	while (pNode)
	{
		pos = 0;
		newHead = pNode;
		while (pNode && pos < k - 1)
		{
			pNode = pNode->next;
			pos++;
		}
		if (pNode)
		{
			pNextNode = pNode->next;
			pNode->next = NULL;
			if (NULL != pLastNode)
			{
				pLastNode->next = NULL;
			}
			pNextNode = ReverseList(newHead);
			if (NULL == pHead)
			{
				pHead = newHead;
			}
			else
			{
				pLastNode->next = newHead;
			}
			pNode = getLastNode(newHead);
			pNode->next = pNextNode;
			pLastNode = pNode;
			pNode = pNextNode;
		}
		else
		{
			break;
		}
	}
	return pHead;
}

void PrintList(Node* pHead)
{
	Node* pNode = pHead;
	while (pNode)
	{
		cout << pNode->val << " ";
		pNode = pNode->next;
	}
	cout << endl;
}

Node* CreateList(int* arr, int len)
{
	Node* pHead = NULL;
	Node* pTemp=pHead;
	Node* pNode=NULL;
	for (int i = 0; i < len; i++)
	{
		pNode = (Node*)malloc(sizeof(Node));
		pNode->val = arr[i];
		pNode->next = NULL;
		if (NULL == pHead)
		{
			pHead = pNode;
		}
		else
		{
			pTemp->next = pNode;
		}
		pTemp = pNode;
	}
	return pHead;
}

void DestroyList(Node* pHead)
{
	Node* pNode;
	while (pHead) {
		pNode = pHead;
		pHead = pHead->next;
		free(pNode);
	}
}

int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int length = sizeof(arr) / sizeof(arr[0]);
	Node* pHead = CreateList(arr, length);
	pHead = swapListByk(pHead, 2);
	PrintList(pHead);
	DestroyList(pHead);
	cin.get();

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

double_happiness

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值