常见的二叉树OJ习题

ps:标题为OJ链接

单值二叉树

如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回 true;否则返回 false。

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
bool isUnivalTree(struct TreeNode* root)
{
    if (root == NULL)
    {
        return true;
    }

    if (root->left && root->left->val != root->val)
    {
        return false;
    }
    if (root->right && root->right->val != root->val)
    {
        return false;
    }

    return isUnivalTree(root->left) && isUnivalTree(root->right);
}

二叉树的最大深度

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明:叶子节点是指没有子节点的节点。

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
int maxDepth(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }

    int left_depth = maxDepth(root->left);
    int right_depth = maxDepth(root->right);
    return left_depth > right_depth ? left_depth + 1 : right_depth + 1;
}

翻转二叉树

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
struct TreeNode* invertTree(struct TreeNode* root)
{
    if (root == NULL)
    {
        return NULL;
    }

    struct TreeNode* tmp = root->left;
    root->left = root->right;
    root->right = tmp;
    invertTree(root->left);
    invertTree(root->right);
    return root;
}

相同的树

给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

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);
}

对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
bool _isSymmetric(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 _isSymmetric(p->left, q->right) && _isSymmetric(p->right, q->left);
}

bool isSymmetric(struct TreeNode* root)
{
    if (root == NULL)
    {
        return true;
    }

    return _isSymmetric(root->left, root->right);
}

二叉树的前序遍历

给定一个二叉树,返回它的前序遍历。
Note: The returned array must be malloced, assume caller calls free()

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
int TreeSize(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + TreeSize(root->left) + TreeSize(root->right);
}

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)
{
    int size = TreeSize(root);
    int* arr = (int*)malloc(sizeof(int) * size);
    *returnSize = size;
    int i = 0;
    _preorderTraversal(root, arr, &i);
    return arr;
}

二叉树的中序遍历

给定一个二叉树,返回它的中序遍历。
Note: The returned array must be malloced, assume caller calls free().

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
int TreeSize(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + TreeSize(root->left) + TreeSize(root->right);
}

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)
{
    int size = TreeSize(root);
    int* arr = (int*)malloc(sizeof(int) * size);
    *returnSize = size;
    int i = 0;
    _inorderTraversal(root, arr, &i);
    return arr;
}

二叉树的后序遍历

给定一个二叉树,返回它的后序遍历。
Note: The returned array must be malloced, assume caller calls free().

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
int TreeSize(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + TreeSize(root->left) + TreeSize(root->right);
}

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)
{
    int size = TreeSize(root);
    int* arr = (int*)malloc(sizeof(int) * size);
    *returnSize = size;
    int i = 0;
    _postorderTraversal(root, arr, &i);
    return arr;
}

平衡二叉树

给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
int Depth(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }

    int left_depth = Depth(root->left);
    int right_depth = Depth(root->right);
    return left_depth > right_depth ? left_depth + 1 : right_depth + 1;
}

bool isBalanced(struct TreeNode* root)
{
    if (root == NULL)
    {
        return true;
    }

    int left_depth = Depth(root->left);
    int right_depth = Depth(root->right);
    return abs(left_depth - right_depth) < 2 && isBalanced(root->left) && isBalanced(root->right);
}

另一个树的子树

给定两个非空二叉树s和t,检验s中是否包含和t具有相同结构和节点值的子树。s的一个子树包括s的一个节点和这个节点的所有子孙。
s也可以看做它自身的一棵子树。

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* s, struct TreeNode* t)
{
    if (s == NULL)
    {
        return false;
    }

    if (isSameTree(s, t))
    {
        return true;
    }
    return isSubtree(s->left, t) || isSubtree(s->right, t);
}

二叉树的构建及遍历

编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。例如如下的先序遍历字符串:
ABC##DE#G##F### 其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
输入描述:
输入包括1行字符串,长度不超过100。
输出描述:
可能有多组测试数据,对于每组数据,
输出将输入字符串建立二叉树后中序遍历的序列,每个字符后面都有一个空格。
每个输出结果占一行。

#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode
{
    char _ch;
    struct TreeNode* _left;
    struct TreeNode* _right;
}TreeNode;

TreeNode* ReBuild(char* ch, int* pi)
{
    if (ch[*pi] == '#')
    {
        return NULL;
    }
    else
    {
        TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
        root->_ch = ch[*pi];
        ++(*pi);
        root->_left = ReBuild(ch, pi);
        ++(*pi);
        root->_right = ReBuild(ch, pi);
        return root;
    }
}

void inorderTraversal(TreeNode* root)
{
    if (root == NULL)
    {
        return;
    }
    inorderTraversal(root->_left);
    printf("%c ", root->_ch);
    inorderTraversal(root->_right);
}

int main()
{
    char ch[100];
    scanf("%s", ch);
    //构建二叉树
    int i = 0;
    TreeNode* root = ReBuild(ch, &i);
    //中序遍历
    inorderTraversal(root);
    return 0;
}

二叉树节点个数

typedef struct BinaryTreeNode
{
    int _data;
    struct BinaryTreeNode* _left;
    struct BinaryTreeNode* _right;
}BTNode;
int BinaryTreeSize(BTNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right);
}

二叉树叶子节点个数

typedef struct BinaryTreeNode
{
    int _data;
    struct BinaryTreeNode* _left;
    struct BinaryTreeNode* _right;
}BTNode;
int BinaryTreeLeafSize(BTNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    if (root->_left == NULL && root->_right == NULL)
    {
        return 1;
    }
    return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}

二叉树第k层节点个数

typedef struct BinaryTreeNode
{
    int _data;
    struct BinaryTreeNode* _left;
    struct BinaryTreeNode* _right;
}BTNode;
int BinaryTreeLevelKSize(BTNode* root, int k)
{
    if (root == NULL)
    {
        return 0;
    }
    if (k == 1)
    {
        return 1;
    }

    return BinaryTreeLevelKSize(root->_left, k - 1) + BinaryTreeLevelKSize(root->_right, k - 1);
}

二叉树销毁

typedef struct BinaryTreeNode
{
    int _data;
    struct BinaryTreeNode* _left;
    struct BinaryTreeNode* _right;
}BTNode;
void BinaryTreeDestory(BTNode* root)
{
    if (root == NULL)
    {
        return;
    }
    BinaryTreeDestory(root->_left);
    BinaryTreeDestory(root->_right);
    free(root);
}

二叉树查找值为x的节点

typedef struct BinaryTreeNode
{
    int _data;
    struct BinaryTreeNode* _left;
    struct BinaryTreeNode* _right;
}BTNode;
BTNode* BinaryTreeFind(BTNode* root, int x)
{
    if (root == NULL)
    {
        return NULL;
    }

    if (root->_data == x)
    {
        return root;
    }

    BTNode* ret = BinaryTreeFind(root->_left, x);
    if (ret)
    {
        return ret;
    }

    ret = BinaryTreeFind(root->_right, x);
    if (ret)
    {
        return ret;
    }
    return NULL;
}

层序遍历二叉树(C++实现)

#include <iostream>
#include <queue>
using namespace std;
struct BTNode
{
    char _data;
    BTNode* _left;
    BTNode* _right;
};
void BinaryTreeLevelOrder(BTNode* root)
{
    queue<BTNode*> q;
    if (root != NULL)
    {
        q.push(root);
    }
    while (!q.empty())
    {
        BTNode* ret = q.front();
        q.pop();
        cout << ret->_data;
        if (ret->_left)
        {
            q.push(ret->_left);
        }
        if (ret->_right)
        {
            q.push(ret->_right);
        }
    }
}

判断二叉树是否是完全二叉树(C++实现)

#include <iostream>
#include <queue>
using namespace std;
struct BTNode
{
    char _data;
    BTNode* _left;
    BTNode* _right;
};
int BinaryTreeComplete(BTNode* root)
{
    queue<BTNode*> q;
    if (root != NULL)
    {
        q.push(root);
    }
    while (!q.empty())
    {
        BTNode* ret = q.front();
        q.pop();
        if (ret == NULL)
        {
            break;
        }
        q.push(ret->_left);
        q.push(ret->_right);
    }

    while (!q.empty())
    {
        if (q.front() != NULL)
        {
            return 0;
        }
        q.pop();
    }
    return 1;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值