• 代码随想录算法训练营第23期day17| 110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和


    目录

    一、(leetcode 110)平衡二叉树

    二、(leetcode 257)二叉树的所有路径

    三、(leetcode 404)左叶子之和


    一、(leetcode 110)平衡二叉树

    力扣题目链接

    状态:已AC

    • 求深度可以从上到下去查,所以需要前序遍历(中左右)
    • 高度只能从下到上去查,所以只能后序遍历(左右中)

    通过这道题,对于树的「高度」和「深度」有了一些体会。高度是相对于叶子节点来说,大部分的递归都需要先走到叶子节点处再向上生长,这就有了高度的概念;而深度则是相对于根节点,在使用迭代方法计算二叉树高度的时候,程序从根节点往下走(层序),就有深度的感觉。

    1. class Solution {
    2. public:
    3.     int getHeight(TreeNode* node){
    4.         if(node == nullptr) return 0;
    5.  
    6.         int leftHeight = getHeight(node->left);
    7.         if(leftHeight == -1) return -1;
    8.         int rightHeight = getHeight(node->right);
    9.         if(rightHeight == -1) return -1;
    10.         return abs(leftHeight - rightHeight) > 1 ? -1 : 1+max(leftHeight, rightHeight);
    11.     }
    12.  
    13.     bool isBalanced(TreeNode* root) {
    14.         int height = getHeight(root);
    15.         return height == -1 ? false : true;
    16.     }
    17. };

    二、(leetcode 257)二叉树的所有路径

    力扣题目链接

    状态:了解思路后Debug AC。

    这道题用了回溯,和前两天的不一样。但是总体思路上是「确定递归终止条件」->「递归前进方式」->「回溯」。注意:「回溯和递归是一一对应的,有一个递归,就要有一个回溯」。这也是最大的收获。

    1. class Solution {
    2. public:
    3.     void travelTree(TreeNode* node, vector<int>& path, vector& res){
    4.         if(node->left == nullptr && node->right == nullptr){
    5.             string rPath = "";
    6.             int len = path.size();
    7.             for(int i = 0; i < len; ++i){
    8.                 rPath += to_string(path[i]);
    9.                 rPath += "->";
    10.             }
    11.             rPath += to_string(node->val);
    12.             res.emplace_back(rPath);
    13.         }
    14.  
    15.         path.emplace_back(node->val);
    16.  
    17.         if(node->left){
    18.             travelTree(node->left, path, res);
    19.             path.pop_back();
    20.         }
    21.         if(node->right){
    22.             travelTree(node->right, path, res);
    23.             path.pop_back();
    24.         }
    25.     }
    26.  
    27.     vector binaryTreePaths(TreeNode* root) {
    28.         vector res;
    29.         vector<int> path;
    30.  
    31.         travelTree(root, path, res);
    32.  
    33.         return res;
    34.     }
    35. };

    三、(leetcode 404)左叶子之和

    力扣题目链接

    状态:Debug后AC

    可以继续利用回溯,回溯的关键就是要用一个数组来记录路径,然后每一次递归操作都要有一次路径的吐出操作(路径的增加在进入递归函数的时候会有)。对于本道题而言,需要注意的求的是左「叶子」节点,不是左孩子节点。

    1. class Solution {
    2. public:
    3.     void travelTree(TreeNode* node, vector& path, int& lsum){
    4.         if(node == nullptr) return;
    5.         path.emplace_back(node);
    6.         if(node->left){
    7.             if(node->left->left == nullptr && node->left->right == nullptr){
    8.                 lsum += node->left->val;
    9.             }
    10.             travelTree(node->left, path, lsum);
    11.             path.pop_back();
    12.         }
    13.         if(node->right){
    14.             travelTree(node->right, path, lsum);
    15.             path.pop_back();
    16.         }
    17.  
    18.     }
    19.  
    20.     int sumOfLeftLeaves(TreeNode* root) {
    21.         // 还是回溯的思想
    22.         vector path;
    23.         int lsum = 0;
    24.         travelTree(root, path, lsum);
    25.         return lsum;
    26.     }
    27. };
  • 相关阅读:
    使用C/C++实现字典树(数组或者链表方式)
    ceph部署
    大数据项目之电商数仓、业务数据介绍、MySQL安装、更改MySQL密码策略
    LabVIEW通过VISA读取或写入时出现超时错误-1073807339
    将fbx文件转换成gltf格式的模型文件
    奇瑞新能源旗下新车——无界Pro上市,雷霆机甲炫酷登场
    1.9.C++项目:仿muduo库实现并发服务器之Connection模块的设计
    面试官:解释下什么是死锁?为什么会发生死锁?怎么避免死锁?
    2022年全球市场胃管泵总体规模、主要生产商、主要地区、产品和应用细分研究报告
    模型实战(21)之 C++ - tensorRT部署yolov8-det 目标检测
  • 原文地址:https://blog.csdn.net/weixin_42179093/article/details/133751240