代码随想录第十三天|222.完全二叉树的节点个数110.平衡二叉树257. 二叉树的所有路径 404.左叶子之和

O(n)版本

class Solution {
public:
    int cot=0;
    void dfs(TreeNode *root){
        if(root==NULL)
          return;
        cot++;
        dfs(root->left);
        dfs(root->right);
    }
    int countNodes(TreeNode* root) {
          if(root==NULL)
           return 0;
           dfs(root);
           return cot;
    }
};

logn logn 时间复杂度

class Solution {
public:
    //完全二叉树判断满二叉树的方式 遍历最左边和最右边高度相等 因为最右边都有节点了 一定都有节点是满二叉树直接返回 否则继续 
    int countNodes(TreeNode* root) {
          if(root==NULL)
             return 0;//结束条件
           else{
              TreeNode *l=root->left;
              int cot1=0;
              TreeNode *r=root->right;
              int cot2=0;
              while(l!=NULL)
               {
                 cot1++;
                 l=l->left;
               }
               while(r!=NULL)
               {
                 cot2++;
                 r=r->right;
               }
               if(cot1==cot2)
                  return (2<<cot1)-1;
                return countNodes(root->left)+countNodes(root->right)+1;
           }
    }
};

思路:完全二叉树性质向左遍历 向右遍历高度一样 是完全二叉树可以直接计算

否则继续递归

class Solution {
public:
    int dfs(TreeNode*root){
        if(root==NULL)
          return 0;
        else{
           int dep1=dfs(root->left);
           int dep2=dfs(root->right);
           return max(dep1,dep2)+1;
        }
    }
    bool isBalanced(TreeNode* root) {
        if(root==NULL)
          return true;
        else{
            int dep1=dfs(root->left);
            int dep2=dfs(root->right);
            if(abs(dep1-dep2)>1)
              return false;
            return isBalanced(root->left)&&isBalanced(root->right);//返回值代表当前树是否为平衡二叉树
        }
    }
};
class Solution {
public:
    vector<string> res;  // 结果集
    vector<int> path;    // 当前路径

    // 深度优先搜索
    void dfs(TreeNode *root) {
        // 如果当前节点是叶子节点
        if (root->left == NULL && root->right == NULL) {
            path.push_back(root->val); 
            // 将路径转换为字符串并加入结果集
            string sPath;
            for (int i = 0; i < path.size(); ++i) {
                sPath += to_string(path[i]);
                if (i != path.size() - 1) sPath += "->";  // 使用"->"连接节点
            }
            res.push_back(sPath);  // 将路径字符串加入结果集
        } else {
            // 递归遍历左右子树
            path.push_back(root->val); 
            if (root->left != NULL) dfs(root->left);
            if (root->right != NULL) dfs(root->right);
        }
        path.pop_back();  // 回溯,移除当前节点
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        if (root == NULL) return res;  // 如果 root 为空,返回空的路径集
        dfs(root);  // 调用深度优先搜索
        return res;  // 返回路径集
    }
};
class Solution {
public:
    int res=0;
    void dfs(int distance,TreeNode *root){
        if(root->left==NULL&&root->right==NULL)
        {
            if(distance==0)
              res+=root->val;
        }else{
            //不是叶子节点
            if(root->left!=NULL){
                dfs(0,root->left);
            }
            if(root->right!=NULL){
                dfs(1,root->right);
            }
        }
    }
    int sumOfLeftLeaves(TreeNode* root) {
         if(root==NULL) return 0;
         dfs(-1,root);
         return res;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值