笔记目录
单链表插入算法:
双向链表插入算法:
双向链表删除元素的操作:
-
[思考题] 单链表的转置:
例:head -> A -> B -> C -> D
解题思路:依次将头节点之后的一位元素删除并插入到尾节点之后,即①head -> B -> C -> D -> A;②head -> C -> D -> B -> A;③head -> D -> C -> B -> A。 -
借楼:
-
回路
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
char val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
private:
char a[30] = {};
char b[30] = {};
int controlNum = 0;
public:
void input(){
cin>>a;
cin>>b;
}
void createTree(TreeNode* result, char array[]){
if (array[controlNum] == '#'){
// cout<<controlNum<<"->"<<"#"<<" ";
result == nullptr;
controlNum ++;
return;
}
result->val = array[controlNum];
// cout<<controlNum<<"->"<<result->val<<" ";
if (++ controlNum <= 30) {
this->createTree(result->left = new TreeNode(), array);
this->createTree(result->right = new TreeNode(), array);
}
}
TreeNode* initA() {
TreeNode* A = new TreeNode();
createTree(A, a);
controlNum = 0;
return A;
}
TreeNode* initB() {
TreeNode* B = new TreeNode();
createTree(B, b);
controlNum = 0;
return B;
}
bool isSameTree(TreeNode* root, TreeNode* subRoot){
if(root == nullptr && subRoot == nullptr){
return true;
}
else if(root == nullptr || subRoot == nullptr){
return false;
}
else if(root->val != subRoot->val){
return false;
}
else{
return isSameTree(root->left, subRoot->left) && isSameTree(root->right, subRoot->right);
}
}
bool dfs(TreeNode* root, TreeNode* subRoot){
if(root == nullptr){
return false;
}
// 当前两棵树是否相同,如果不相同,判断root左子树或者右子树是否和subRoot相同
return isSameTree(root, subRoot) || dfs(root->left, subRoot) || dfs(root->right, subRoot);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
return dfs(root, subRoot);
}
};
int main() {
Solution solution;
solution.input();
TreeNode* A = solution.initA();
TreeNode* B = solution.initB();
bool result = solution.isSubtree(A, B);
// cout<<result;
if(result == 1){
cout<<"yes";
}
else{
cout<<"no";
}
return 0;
}
- 二叉树建立:
#include<stdio.h>
#include<stdlib.h>
typedef struct BinaryTreeNode
{
int data;
struct BinaryTreeNode *Left;
struct BinaryTreeNode *Right;
}Node;
//创建二叉树,顺序依次为中间节点->左子树->右子树
Node* createBinaryTree()
{
Node *p;
int ch;
printf("输入data");
scanf("%d",&ch);
if(ch == 0) //如果到了叶子节点,接下来的左、右子树分别赋值为0
{
p = NULL;
}
else
{
p = (Node*)malloc(sizeof(Node));
p->data = ch;
p->Left = createBinaryTree(); //递归创建左子树
p->Right = createBinaryTree(); //递归创建右子树
}
return p;
}
//先序遍历
void preOrderTraverse(Node* root)
{
if( root )
{
printf("%d",root->data);
preOrderTraverse(root->Left);
preOrderTraverse(root->Right);
}
}
//中序遍历
void inOrderTraverse(Node* root)
{
if( root )
{
inOrderTraverse(root->Left);
printf("%d",root->data);
inOrderTraverse(root->Right);
}
}
//后序遍历
void lastOrderTraverse(Node* root)
{
if( root )
{
lastOrderTraverse(root->Left);
lastOrderTraverse(root->Right);
printf("%d",root->data);
}
}
//二叉树节点总数目
int Nodenum(Node* root)
{
if(root == NULL)
{
return 0;
}
else
{
return 1+Nodenum(root->Left)+Nodenum(root->Right);
}
}
//二叉树叶子节点数
int Leafnum(Node* root)
{
if(!root)
{
return 0;
}
else if( (root->Left == NULL) && (root->Right == NULL) )
{
return 1;
}
else
{
return (Leafnum(root->Left) + Leafnum(root->Right)) ;
}
}
int main()
{
int i;
Node *root = NULL;
root = createBinaryTree();
printf("二叉树建立成功");
printf("\n");
preOrderTraverse(root);
printf("\n");
inOrderTraverse(root);
printf("\n");
lastOrderTraverse(root);
printf("\n");
i = Leafnum(root);
printf("%d",i);
printf("\n");
i = Nodenum(root);
printf("%d",i);
return 0;
}
2022.05.07
今天看到基数排序,原算法是从低位向高位计算的,感觉这样子有点繁琐: