leetcode刷题.226. 669. 100. 501二叉树easy基础

本文深入探讨了二叉树的多种操作与算法实现,包括树的翻转、修剪、比较以及查找二叉树中的众数。通过具体的代码示例,详细解释了如何使用递归方法来解决这些树形结构的问题。

226.Invert The Binary Tree 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    void reverseTree(TreeNode *node)
    {
        TreeNode *tmp_ = node->right;
        node->right = node->left;
        node->left = tmp_;

        if(nullptr != node->left) reverseTree(node->left);
        if(nullptr != node->right) reverseTree(node->right);
    }

    TreeNode* invertTree(TreeNode* root) {

        if(nullptr == root)
            return root;

        reverseTree(root);

        return root;
    }
};

669 Trim The Bianry Tree

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int L, int R) {

        if(nullptr == root)
            return root;

        if(root->val > R)
            return trimBST(root->left, L, R);

        if(root->val < L)
            return trimBST(root->right, L, R);

        root->left = trimBST(root->left, L, R);
        root->right = trimBST(root->right, L, R);
        return root;       
    }
};

100. Is Same Binary Tree

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        
        if(nullptr == p && nullptr == q)
            return true;

        if((nullptr == p && nullptr != q) ||
              (nullptr == q && nullptr != p))
           return false;

        if(p->val != q->val)
            return false;

        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};

501.二叉树中的众数

 struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
 

 void middleSearch(TreeNode* root, vector<int>& out, int& preVal, int& preCount, int& count)
 {
         if (nullptr == root) {
                 return;
         }

         middleSearch(root->left, out, preVal, preCount, count);

         // visit
         if (count <= 0) {

                 preVal = root->val;
                 count = 1;
         }
         else {

                 if (root->val != preVal) {

                         if (count > preCount) {

                                 out.clear();
                                 out.push_back(preVal);
                                 preCount = count;
                         }
                         else if (count == preCount) {
                                 out.push_back(preVal);
                         }

                         preVal = root->val;
                         count = 1;
                 }
                 else
                         count++;
         }

         middleSearch(root->right, out, preVal, preCount, count);
 }

 vector<int> findMode(TreeNode* root) {

         vector<int> out_;
         int preVal_ = 0;
         int preCount_ = 0, count_ = 0;

         if (nullptr == root)
                 return out_;

         middleSearch(root, out_, preVal_, preCount_, count_);

         // s收尾处理
         if (count_ > preCount_) {

                 out_.clear();
                 out_.push_back(preVal_);
         }
         else if (count_ == preCount_) {

                 out_.push_back(preVal_);
         }

         return out_;
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值