【数据结构】二叉树相关题目

一、单值二叉树

两边同时遍历,边遍历边进行比较,完成遍历后返回真,否则为假。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isUnivalTree(struct TreeNode* root) 
{
    if(root == NULL)
    {
        return true;
    }    

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

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

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

二、二叉树的最大深度

与求深度同理

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int maxDepth(struct TreeNode* root) 
{
    if(root == NULL)
    {
        return 0;
    }    

    int leftroot = maxDepth(root->left);
    int rightroot = maxDepth(root->right);

    return leftroot > rightroot ? leftroot+1 : rightroot+1;
}

 三、翻转二叉树

 使用后序遍历的思想,先遍历,后调整

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* invertTree(struct TreeNode* root) 
{
    if(root == NULL)
    {
        return 0;
    }

    struct TreeNode* leftroot = invertTree(root->left);
    struct TreeNode* rightroot = invertTree(root->right);

    root->left = rightroot;
    root->right = leftroot;

    return root;
}

 四、相同的树

        在递归时每一层函数的栈帧中存在这样的条件:p 为空,q 也为空,返回 true;p 或者 q 只有一个为空,返回 false;p 和 q 的 val 不等,返回 false;否则 p 和 q 的 val 是相等的才递归

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

五、对称二叉树

一棵树对称就是 它的左右子树呈镜像状态。说白了就是节点左子树的值等于右子树的值右子树的值等于左子树的值。把左右看成两个独立的树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool _isSymmetric(struct TreeNode* Leftroot, struct TreeNode* Rightroot)
{
    if(Leftroot == NULL && Rightroot == NULL)
    {
        return true;
    }

    if(Leftroot == NULL || Rightroot == NULL)
    {
        return false;
    }

    if(Leftroot->val != Rightroot->val)
    {
        return false;
    }

    return _isSymmetric(Leftroot->left, Rightroot->right) && _isSymmetric(Leftroot->right, Rightroot->left);
}

bool isSymmetric(struct TreeNode* root) 
{
    return _isSymmetric(root->left,root->right);

 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值