leetcode树的刷题笔记

94 给定一个二叉树的根节点 root ,返回它的 中序 遍历。//我不会树的递归在慢慢学了

class Solution {
public:
    void inorder(TreeNode* root, vector<int>& res) {
        if (!root) {
            return;
        }
        inorder(root->left, res);//递归左子树
        res.push_back(root->val);//并且把节点压倒数组中去
        inorder(root->right, res);//递归右子树
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root, res);
        return res;
    }
};

100.给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。(我不会做我不太理解树这的递归)

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if (!p && !q) {
            return true;
        } else if (!p || !q) {
            return false;
        } else if (p->val != q->val) {
            return false;
        } else {
            return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);//
//大致理解一点就是我在p和q判断完了之后要接着判断p的左子树q的左子树p的右子树q的右子树
        }
    }
};

大致对递归有个理解

我写p和q相同之后要接着判断p的左子树q的左子树p的右子树q的右子树

就是我在判断p和q的关系时我要判断p,q的后面的关系好像就写出来了

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

感觉大致理解了一点递归好像就是要执行现在这个步骤的下一步的时候就用到了递归

就写就可以了

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        // 对称 左==右
        if (!root){
            return true;
        }
        return ismirror(root->left, root->right);
    }
    bool ismirror(TreeNode *p, TreeNode *q){
        if (p && !q){
            return false;
        }else if (!p && q){
            return false;
        }else if (!p && !q){
            return true;
        }else if (p->val == q->val){
            return ismirror(p->left, q->right) && ismirror(q->left, p->right);
        }else{
            // 左 == 右
            // 递归 看清楚谁和谁相比
            return false;
        }
    }
};

 感觉错了我还是不会做

104.给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(!root){
            return 0;
        }
        else{
            return max(maxDepth(root->left),maxDepth(root->right))+1;
        }
    }
};

 理解了但没完全理解就是我一个结点之后我要在找他的左节点的深度和右结点的深度(这时递归存在)还得加1(应为返回值后确实每次都比原来少一位  )就是说当他为一层时他就是2个长度而我的算的只有一层所以要加1所以一直要加1

就是你一直在用你以前的步骤这个return必须满足每一次

to_string 函数:将数字常量转换为字符串,返回值为转换完毕的字符串

头文件:#include<string>

以后每天刷两道leetcode上的题

 11月21日:(翻转二叉树和二叉树的所有路径)

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        TreeNode *p;
        if(!root){
            return NULL;
        }
        p=root->left;
        root->left=root->right;//反转它的左子树和右子树
        root->right=p;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};
//这题有点水啊哈哈

 

class Solution {
public:
     void traversal(TreeNode *cur,vector<int>&path,vector<string>&result)
    {
        if(cur){
            if(!cur->left&&!cur->right){//当他是叶子节点的时候
                string sPath;//建立一个
                for(int i=0;i<=path.size()-1;i++){
                    sPath+=to_string(path[i]);//将数字转化成字符
                    sPath+="->";
                }
                sPath+=to_string(cur->val);
                result.push_back(sPath);
                return ;
        }
        else{
            path.push_back(cur->val);
            traversal(cur->left,path,result);
            traversal(cur->right,path,result);
        }
        path.pop_back();
        }
       
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string>result;//容器嵌套
        vector<int>path;
        if(!root){
            return result;
        }
        if(!root->left&&!root->right){
            result.push_back(to_string(root->val));
            return result;
        }
        traversal(root,path,result);
        return result;
    }
    //STL的嵌套
   
//vector<string>result是最终返回值用的是容器的嵌套;
};

11月22日(左叶子之和,二叉树的直径)

水题真开心哈哈

class Solution {
public:
int count=0;
    int sumOfLeftLeaves(TreeNode* root) {
        if(!root){
            return 0;
        }
        if(root->left&&!root->left->left&&!root->left->right){
            count+=root->left->val;
        }
        sumOfLeftLeaves(root->left);
        sumOfLeftLeaves(root->right);
        return count;
    }
};

不会做。。。。。

  1. 二叉树的直径不一定过根节点,因此需要去搜一遍所有子树(例如以root,root.left, root.right...为根节点的树)对应的直径,取最大值。
  2. root的直径 = root左子树高度 + root右子树高度
  3. root的高度 = max {root左子树高度, root右子树高度} + 1
class Solution {

    public:
     int maxn = 0;
     int diameterOfBinaryTree(TreeNode* root) {
        dfs(root);
        return maxn;
    }
     int dfs(TreeNode * root) {
        if (root==NULL) {
            return 0;
        }
        int leftHeight = dfs(root->left), rightHeight = dfs(root->right);//先递归到树的下方在退栈把每个结点当作根结点求他的最大深度
        maxn = max(leftHeight + rightHeight, maxn);
        return max(leftHeight, rightHeight) + 1;//返回节点深度
    }
};

11月23日(二叉树的层平均值,合并二叉树):

class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        queue<TreeNode*>Q;
        if(!root) Q.push(root);  
        vector<double> num;
        while(!Q.empty()){
            double sum=0;int size=Q.size();
            for(int  i=0;i<size;i++){
                TreeNode *p=Q.front();
                Q.pop();
                sum+=p->val;
            if(p->left) Q.push(p->left);
            if(p->right) Q.push(p->right); 
        }
        num.push_back(sum/size);
        }
        return num;
    }
};
//层序遍历没那么难

//思路感觉很简单不知道为啥没做对哎太菜了
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        if(!t1&&!t2) return NULL;
        if (t1 == NULL) return t2; // 如果t1为空,合并之后就应该是t2
        if (t2 == NULL) return t1; // 如果t2为空,合并之后就应该是t1
        // 修改了t1的数值和结构
        t1->val += t2->val;                             // 中
        t1->left = mergeTrees(t1->left, t2->left);      // 左
        t1->right = mergeTrees(t1->right, t2->right);   // 右
        return t1;
    }
};
//应用了一个先序遍历

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李小于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值