打卡代码随想录第15天:LeetCode 110.平衡二叉树 257. 二叉树的所有路径 404.左叶子之和

学习资料:代码随想录

文中含LLM生成内容

110.平衡二叉树

力扣题目链接

思路:逐层返回当前节点的最大高度,比较各节点的左右孩子高度

后续方法遍历,因为‘中’是比较环节,要在左右之后

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int getHeight(TreeNode* node){
        if(node==NULL) return 0;
        int leftHeight = getHeight(node->left);
        if(leftHeight==-1) return -1;
        int rightHeight = getHeight(node->right);
        if(rightHeight==-1) return-1;
        int result;
        if(abs(leftHeight-rightHeight)>1){
            result=-1;         //后序方法,左右遍历得到leftHeight和rightHeight之后才能有这里的‘中’位置的比较
        }
        else{
            result = 1+max(leftHeight,rightHeight);
        }
        return result;                                       //因为在这里一定得有个返回值,所以别把逻辑都写在if或else里
    }
    bool isBalanced(TreeNode* root) {
        return getHeight(root)==-1 ? false:true;
    }
};

257. 二叉树的所有路径

力扣题目链接

思路:返回的是val和箭头,要按字符串处理箭头,前序,递归到底时,把数字放进一个数组里,把这个数组转移到字符串,再转移到结果。

关键点在回溯的时候要及时把数组弹出上一节点的值,以便添加新的分支的节点的值

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    void traversal(TreeNode* cur,vector& path,vector& result){           //path不为string类型以防遇到多位的数
        path.push_back(cur->val);                                    //根据后面的逻辑,确保叶子节点的值也被加入到path
        if(cur->left==NULL&&cur->right==NULL){                  //从叶子节点返回,判断左右孩子都为空了
            string sPath;
            //int size=path.size();
            for(int i=0;i" 是字符串字面量,表示由 - 和 > 组成的字符串。'->'(单引号)在 C++ 中无效,因为单引号只能用于单个字符char,例如 '-' 或 '>'
            }
            sPath+=to_string(path[path.size()-1]);
            result.push_back(sPath); 
            return;                                            //别忘了return
        }
        if(cur->left){
            traversal(cur->left,path,result);
            path.pop_back();                                    //弹出记录过的值,以进新的
        }
        if(cur->right){
            traversal(cur->right,path,result);
            path.pop_back();                                    //弹出记录过的值,以进新的
        }

    }
public: 
    vector binaryTreePaths(TreeNode* root) {
        vector result;
        vector path;
        if(root==NULL) return result;
        traversal(root,path,result);
        return result;
    }
};

404.左叶子之和

力扣题目链接

思路:遇到空回溯返回0,遇到叶子也是返回0,关键点在遇到左叶子的父节点返回的左叶子的值,这才是答案的构成部分

把返回叶子节点的值放在左递归完成回溯到右递归中间,因为就要个左叶子

后序 ,‘中’是返回的所有回溯回来的左叶子的和

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(root==NULL) {return 0;}
        if(root->left==NULL&&root->right==NULL){       //其实这个也可以不写,如果不写不影响结果,但就会让递归多进行了一层。但是写上不会影响叶子节点的返回,因为返回的值是->left->val,从父节点取的
            return 0;
        }
        int leftValue = sumOfLeftLeaves(root->left);
        if(root->left&&!root->left->left&&!root->left->right){
            leftValue = root->left->val;
        }
        int rightValue =sumOfLeftLeaves(root->right);
        int sum = leftValue+rightValue;
        return sum;
    }
};

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

力扣题目链接

思路:遍历一遍累计和

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int traversal(TreeNode* cur){
        if(cur==nullptr){
            return 0;
        }
        int leftSum = traversal(cur->left);
        int rightSum = traversal(cur->right);
        int sum = leftSum+rightSum+1;
        return sum;
    }
    int countNodes(TreeNode* root) {
        return traversal(root);
    }
};

对于完全二叉树,可借助其性质通过判断是否有满二叉树存在来计算,因为满二叉树的节点数量和是(2^n)-1;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;;
        int leftDepth=0,rightDepth=0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        while(left){
            left = left->left;
            leftDepth++;
        } 
        while(right){
            right= right->right;
            rightDepth++;
        }
        return 1+countNodes(root->left)+countNodes(root->right);
    }
};

时间复杂度O(logn*logn)

  • 计算深度 的时间是 O(log n),因为完全二叉树的深度是对数级别的。
  • 递归调用的次数 也是 O(log n),因为每次递归都会将问题规模减半(即递归到左右子树)。

层序遍历也很合适:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int countNodes(TreeNode* root) {
        int result=0;
        queue que;
        if(root!=NULL) que.push(root);
        while(!que.empty()){
            int size= que.size();
            for(int i=0;ileft) que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return result;
    }
};

你可能感兴趣的:(leetcode)