letcode 分类练习 110. 平衡二叉树 257. 二叉树的所有路径 404. 左叶子之和 222. 完全二叉树的节点个数

letcode 分类练习 110. 平衡二叉树 101. 对称二叉树 104.二叉树的最大深度 111.二叉树的最小深度

  • [110. 平衡二叉树](#110. 平衡二叉树)
  • [257. 二叉树的所有路径](#257. 二叉树的所有路径)
  • [404. 左叶子之和](#404. 左叶子之和)
  • [222. 完全二叉树的节点个数](#222. 完全二叉树的节点个数)
  1. 对称二叉树 104.二叉树的最大深度 111.二叉树的最小深度)

110. 平衡二叉树

用递归的思路检查左子树和右子树的高度差绝对值是不是在1以内,为了方便同时获取左子树和右子树的高度,我们定义如果高度为-1表示该子树不满足平衡二叉树,如果不等于-1表示子树的高度

c 复制代码
class Solution {
public:
    int height(TreeNode* node){
        if(!node) return 0;
        int left_depth = height(node -> left);
        int right_depth = height(node -> right);
        if(left_depth == -1 || right_depth == -1 || abs(left_depth - right_depth) > 1)return -1;
        else return max(left_depth, right_depth) + 1;
    }
    bool isBalanced(TreeNode* root) {
        return height(root) >= 0;
    }
};

257. 二叉树的所有路径

每个节点都对应唯一的从根节点出发的路径,只需要做一个遍历,如果它是叶子节点就记录路径即可

c 复制代码
class Solution {
public:
    vector<string> result;
    void dfs(TreeNode* root, string s){
        if(!root) return;
        if(!root->left && !root->right)result.push_back(s);
        if(root->left)dfs(root -> left, s + "->" + to_string(root -> left -> val));
        if(root->right)dfs(root -> right, s + "->" + to_string(root -> right -> val));
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        if(!root) return result;
        if(!root -> left&& !root->right){result.push_back(to_string(root->val)); return result;}
        dfs(root, to_string(root->val));
        return result;
    }
};

404. 左叶子之和

传参的时候可以告诉该节点是左孩子还是右孩子,再判断一下当前节点是不是叶子结点即可

c 复制代码
class Solution {
public:
    int sum = 0;
    void dfs(TreeNode* root, int flag){
        if(!root) return;
        if(!root -> left && !root -> right && flag == 0)sum+= root->val;
        dfs(root->left, 0);
        dfs(root ->right, 1);
    }
    int sumOfLeftLeaves(TreeNode* root) {
        dfs(root, -1);
        return sum;
    }
};

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

可以利用完全二叉树的性质解题

完全二叉树是一定要按照一层一层的顺序装填的,所以判断完全二叉树只需要一直向左和一直向右迭代,两边的深度一样即可,注意下面的不是完全二叉树:

c 复制代码
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};
相关推荐
tobias.b13 分钟前
408真题解析-2010-7-数据结构-无向连通图
数据结构·算法·图论·计算机考研·408真题解析
白日做梦Q1 小时前
细粒度图像分类:从双线性CNN到TransFG的技术演进
分类·数据挖掘·cnn
良木生香1 小时前
【鼠鼠优选算法-双指针】003:快乐数 & 004:盛水最多的容器
算法
Cx330❀1 小时前
【优选算法必刷100题】第41-42题(模拟):Z 字形变换,外观数列
c++·算法
沃尔特。1 小时前
直流无刷电机FOC控制算法
c语言·stm32·嵌入式硬件·算法
CW32生态社区1 小时前
CW32L012的PID温度控制——算法基础
单片机·嵌入式硬件·算法·pid·cw32
Cx330❀1 小时前
【优选算法必刷100题】第038题(位运算):消失的两个数字
开发语言·c++·算法·leetcode·面试
漫随流水1 小时前
leetcode回溯算法(93.复原IP地址)
数据结构·算法·leetcode·回溯算法
燃于AC之乐1 小时前
我的算法修炼之路--5——专破“思维陷阱”,那些让你拍案叫绝的非常规秒解
c++·算法·贪心算法·bfs·二分答案·扩展域并查集·动态规划(最长上升子序列)
艾莉丝努力练剑1 小时前
【优选算法必刷100题】第021~22题(二分查找算法):山脉数组的峰顶索引、寻找峰值
数据结构·c++·算法·leetcode·stl