一、二叉树的前序遍历
思路:先求出树的结点个数,开好数组之后进行前序遍历即可。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int TreeSide(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
return TreeSide(root->left) + TreeSide(root->right) + 1;
}
void _preorderTraversal(struct TreeNode* root, int*arr, int*pi)
{
if(root == NULL)
{
return;
}
arr[(*pi)++] = root->val;
_preorderTraversal(root->left,arr,pi);
_preorderTraversal(root->right,arr,pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = TreeSide(root);
int* arr = (int*)malloc(*returnSize * sizeof(int));
int i = 0;
_preorderTraversal(root,arr,&i);
return arr;
}
二、二叉树的中序遍历
思路:先求出树的结点个数,开好数组之后进行中序遍历即可。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int Treeside(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
return Treeside(root->left) + Treeside(root->right) + 1;
}
void _inorderTraversal(struct TreeNode* root, int*arr, int* pi)
{
if(root == NULL)
{
return;
}
_inorderTraversal(root->left, arr, pi);
arr[(*pi)++] = root->val;
_inorderTraversal(root->right, arr, pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = Treeside(root);
int* arr = (int*)malloc(*returnSize * sizeof(int));
int i = 0;
_inorderTraversal(root,arr,&i);
return arr;
}
三、二叉树的后序遍历
思路:先求出树的结点个数,开好数组之后进行后序遍历即可。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int Treeside(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
return Treeside(root->left) + Treeside(root->right) + 1;
}
void _postorderTraversal(struct TreeNode* root, int*arr, int* pi)
{
if(root == NULL)
{
return;
}
_postorderTraversal(root->left, arr, pi);
_postorderTraversal(root->right, arr, pi);
arr[(*pi)++] = root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = Treeside(root);
int* arr = (int*)malloc(*returnSize * sizeof(int));
int i = 0;
_postorderTraversal(root,arr,&i);
return arr;
}
四、另一颗树的子树
思路:每一层函数栈帧中都包括,如果 root 等于空,返回 false,如果调用相同的树为真,返回 true。每一层往下判断是否为相同的树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
if(p == NULL && q == NULL)
{
return true;
}
if(p == NULL || q == NULL)
{
return false;
}
if(p->val != q->val)
{
return false;
}
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
{
if(root == NULL)
{
return false;
}
if(isSameTree(root, subRoot))
{
return true;
}
return isSubtree(root->left,subRoot) || isSubtree(root->right, subRoot);
}
五、二叉树遍历
先前序构建树,再中序输出树即可
#include <stdio.h>
#include <stdlib.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if(node == NULL)
{
perror("BuyNode");
return NULL;
}
node->left = NULL;
node->right = NULL;
node->data = x;
return node;
}
BTNode* CreateBTree(char a[], int* pi)
{
if(a[*pi] == '#')
{
(*pi)++;
return NULL;
}
BTNode* root = BuyNode(a[*pi]);
(*pi)++;
root->left = CreateBTree(a,pi);
root->right = CreateBTree(a,pi);
return root;
}
void InOrder(BTNode* root)
{
if(root == NULL)
{
return;
}
InOrder(root->left);
printf("%c ",root->data);
InOrder(root->right);
}
int main()
{
char a[100] = {0};
scanf("%s",a);
int i = 0;
BTNode* root = CreateBTree(a,&i);
InOrder(root);
printf("\n");
return 0;
}
六、平衡二叉树
思路:用求树深度的方式判断每个节点的子树的高度差是否小于等于1即可。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int TreeHeap(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
int leftnode = TreeHeap(root->left);
int rightnode = TreeHeap(root->right);
return leftnode>rightnode ? leftnode+1 : rightnode+1;
}
bool isBalanced(struct TreeNode* root)
{
if(root == NULL)
{
return true;
}
int lefttree = TreeHeap(root->left);
int righttree = TreeHeap(root->right);
if(abs(lefttree - righttree) > 1)
{
return false;
}
return isBalanced(root->left) && isBalanced(root->right);
}