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

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

LeetCode 110.平衡二叉树

题目链接:LeetCode 110.平衡二叉树

思路:
1.递归法 分别求左右,取差的绝对值。

//递归法
class Solution {
public:

    int getHeight(TreeNode* node){
        if(!node) return 0;
        int leftHeight = getHeight(node->left);
        if (leftHeight == -1) return -1;
        int rightHeight = getHeight(node->right);
        if (rightHeight == -1) return -1;

        return abs(leftHeight-rightHeight) <= 1 ? 1+max(leftHeight, rightHeight): -1;

    }
    bool isBalanced(TreeNode* root) {
        if(!root) return true;
        return getHeight(root) == -1 ? false : true;
    }
};

LeetCode 257. 二叉树的所有路径

题目链接:LeetCode 257. 二叉树的所有路径

思路:
1.分别传入path和res

class Solution {
public:

    void traversal(TreeNode* node, vector<int>& path, vector<string>& res){
        path.push_back(node->val);
        if(!node->left && !node->right){
            string sPath;
            for(int p:path) sPath += to_string(p)+"->";
            sPath.pop_back();
            sPath.pop_back();
            res.push_back(sPath);
            return;
        }

        if(node->left) {
            traversal(node->left, path, res);
            path.pop_back();
        }

        if(node->right) {
            traversal(node->right, path, res);
            path.pop_back();
        }
        
    }

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

//传值法不需要pop

class Solution {
public:

    void traversal(TreeNode* node, string path, vector<string>& res){
        path += to_string(node->val);
        if(!node->left && !node->right){
            res.push_back(path);
            return;
        }
        if(node->left) traversal(node->left, path+"->", res);
        if(node->right) traversal(node->right, path+"->", res);
        
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        string path;
        vector<string> res;
        if(!root) return res;
        traversal(root, path, res);
        return res;
    }
};

注意 :

  1. 注意path和res都是传地址,用&
  2. 如果path传值,不需要pop

LeetCode 404.左叶子之和

题目链接:LeetCode 404.左叶子之和

思路:
通过父节点判断是否为左叶子

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if (root == NULL) return 0;
        if (root->left == NULL && root->right== NULL) return 0;
        int sum = 0;
        sum += sumOfLeftLeaves(root->left);    // 左
        sum += sumOfLeftLeaves(root->right);  // 右
        if (root->left && !root->left->left && !root->left->right) { // 左子树就是一个左叶子的情况
            sum += root->left->val;
        }
        return sum;
    }
};

注意 :

  1. 注意收集左孩子节点
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值