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

代码随想录算法训练营第X天

110.平衡二叉树

题目链接:110.平衡二叉树
计算左右子树高度的差值如果大于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 getDepth(TreeNode* root){
        if(root == nullptr)return 0;
        int result = 0;
        int leftDep = getDepth(root->left); //左
        int rightDep = getDepth(root->right); //右
        if(leftDep==-1||rightDep==-1)return -1; //提前剪枝
        if(abs(leftDep-rightDep)>1) result =-1;//在求深度的递归中检查子树是否为平衡树。
        else result = max(leftDep,rightDep)+1;
        return result;
    }
    bool isBalanced(TreeNode* root) {
        if(root == nullptr)return true;
        if(getDepth(root)==-1)return false;
        else return true;
    }
};

//方法二,子函数只递归计算高度,主函数里再递归检查子树是否是平衡二叉树。
class Solution {
public:
    int getDepth(TreeNode* root){
        if(root == nullptr)return 0;
        int result = 0;
        int leftDep = getDepth(root->left);
        int rightDep = getDepth(root->right);
        result = max(leftDep,rightDep)+1;
        return result;
    }
    bool isBalanced(TreeNode* root) {
        if(root == nullptr)return true;
        if(getDepth(root)==-1)return false;
        int leftDep = getDepth(root->left);
        int rightDep = getDepth(root->right);
        if(abs(leftDep-rightDep)>1) return false;
        if (!isBalanced(root->left)||!isBalanced(root->right))return false;
        else return true;
    }
};

257. 二叉树的所有路径

题目链接:257. 二叉树的所有路径
前序遍历,先遍历到叶子节点将路径加入结果集,然后回溯到前一个根寻找下一个叶子,再连续向上回溯。

class Solution {
public:
    void construct_paths(TreeNode* root,string path,vector<string>& paths){
        if(!root)return;
        path+=to_string(root->val);
        //判断是否到叶子节点,到了就把路径加入。没到就继续左右遍历
        if(root->left==nullptr&&root->right==nullptr){
            paths.push_back(path);
        }else{
            path+="->";
            construct_paths(root->left,path,paths); //按值传递,每次都传进来一个新的字符串,所以不用删除回溯。如果传入引用,就要在后面pop_back()3次,将已经传入的值以及->,从字符串中移除
            construct_paths(root->right,path,paths);
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        construct_paths(root, "", result);
        return result;
    }
};

404.左叶子之和

题目链接:404.左叶子之和
关键如何判断左叶子。只能通过左叶子的父节点判断,该节点(左叶子的父节点)的左子节点不空(判断左)同时左节点的左子结点和右子节点都为空。(判断叶子节点)

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(!root)return 0;
        int leftNum = sumOfLeftLeaves(root->left);
        int rightNum =sumOfLeftLeaves(root->right);
        //判断是否是左叶子节点,如果是就加和。
        if(root->left!=nullptr&&root->left->left==nullptr&&root->left->right==nullptr) {
        	leftNum+=root->left->val;
        	}
        int sum =leftNum+rightNum; //左子树的左叶子之和+右子树的左叶子之和=全部左叶子之和
        return sum;
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值