DAY15 - 110.平衡二叉树, 257. 二叉树的所有路径 , 404.左叶子之和, 222.完全二叉树的节点个数

110.平衡二叉树

第一反应是跟求最大深度很像,在每个步骤中加入判断,如果任意哪一层高度差大于1,则返回错误。

有个小tips是如何又返回判断结果又返回深度?=》函数设置为int类型,如果符合要求则返回高度,如果不符合则返回-1

class Solution {
public:
    int getd(TreeNode* root){
        if(root==nullptr) return 0;
        int ld=getd(root->left);
        int rd=getd(root->right);
        if(ld==-1||rd==-1||ld-rd>1||rd-ld>1) return -1;
        return 1+max(ld,rd);
    }

    bool isBalanced(TreeNode* root) {
        if(getd(root)==-1) return false;
        else return true;
    }
};

257. 二叉树的所有路径

当递归到叶子结点的时候创建一个string path,单独加入val,然后每层递归结束回到上一层再加上 val->.

但这样是有问题的,在递归中创建的临时变量会在递归结束消失,所以要把它当成参数传递。

class Solution {
public:
    void path(TreeNode* root, string currentPath, vector<string>& res) {
        if (!root) return;
        if (!root->left && !root->right) {
            currentPath += to_string(root->val);
            res.push_back(currentPath);
            return;
        }
        currentPath += to_string(root->val) + "->";
        path(root->left, currentPath, res);
        path(root->right, currentPath, res);
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        if (!root) return res;
        
        path(root, "", res);
        
        return res;
    }
};

404.左叶子之和

左叶子就是它自己是left并且没有子。

class Solution {
public:
    void sumll(TreeNode* root, int& sum){
        if(root==nullptr) return;
        if(root->left&&root->left->left==nullptr&&root->left->right==nullptr){
            sum+=root->left->val;
        }
        sumll(root->left,sum);
        sumll(root->right,sum);

    }

    int sumOfLeftLeaves(TreeNode* root) {
        int sum=0;
        sumll(root,sum);
        return sum;
    }
};
  • 引用(reference) 是 C++ 中的一种特性,它允许你创建一个变量的别名,给定的引用和原始变量是相同的,它们共享同一块内存空间。使用引用,你可以直接修改原始变量的值,而不需要通过指针或复制变量的副本。

以下代码是等价的(c和c++)

#include <stdio.h>

void sumll(int* sum) {
    *sum += 10;  // 修改 sum 的值,必须通过指针解引用
}

int main() {
    int sum = 0;
    sumll(&sum);  // 传递 sum 的地址
    printf("%d\n", sum);  // 输出:10
    return 0;
}
#include <iostream>
using namespace std;

void sumll(int& sum) {
    sum += 10;  // 修改 sum 的值,直接使用引用
}

int main() {
    int sum = 0;
    sumll(sum);  // 传递 sum 的引用
    cout << sum << endl;  // 输出:10
    return 0;
}

222.完全二叉树的节点个数

普通二叉树统计结点个数就是遍历一遍,完全二叉树可以简化这个过程。

使用的特性:1. 满二叉树的结点个数 2^n-1 n是深度。 2. 完全二叉树通过一直向下走左边或者一直向下走右边看深度就可以判断它是否是满二叉树。 3.

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(!root) return 0;

        int ld=1,rd=1;
        TreeNode* curl=root->left;
        TreeNode* curr=root->right;
        while(curl){
            ld++;
            curl=curl->left;
        }
        while(curr){
            rd++;
            curr=curr->right;
        }

        if(ld==rd) return pow(2, ld)-1;
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

最后这句 return countNodes(root->left) + countNodes(root->right) + 1; 是简化写法,完整写法是

int leftTreeNum = countNodes(root->left);       // 左
int rightTreeNum = countNodes(root->right);     // 右
int result = leftTreeNum + rightTreeNum + 1;    // 中
return result;
posted @ 2025-04-03 11:26  ChloeChen0221  阅读(7)  评论(0)    收藏  举报