二叉树的路径


二叉树所有路径

题目链接
在这里插入图片描述

/**
 * 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 {
    vector<string> res;
    void backtrace(TreeNode* root, string s){
        if(!root) return;
        s+=to_string(root->val);
        if(!root->left && !root->right){
            res.push_back(s);
            return;
        }
        backtrace(root->left, s+"->");
        backtrace(root->right, s+"->");
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        backtrace(root, "");
        return res;
    }
};

路径总和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 {
    bool backtrace(TreeNode* root, int sum){
        sum -= root->val;
        if(!root->left && !root->right && sum == 0){
            return true;
        }
        if(root->left && backtrace(root->left, sum)) return true;
        if(root->right && backtrace(root->right, sum)) return true;
        return false;
    }
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(!root) return false;
        if(backtrace(root, targetSum)) return true;
        return false;
    }
};

路径总和2

题目链接
在这里插入图片描述

/**
 * 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 {
    vector<vector<int>> res;
    vector<int> temp;
    void backtrace(TreeNode* root, int sum){
        sum -= root->val;
        temp.push_back(root->val);
        if(!root->left && !root->right && sum == 0){
            res.push_back(temp);
            return;
        }
        if(root->left){
            backtrace(root->left, sum);
            temp.pop_back();
        } 
        if(root->right){
            backtrace(root->right, sum);
            temp.pop_back(); 
        } 
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root) return {};
        backtrace(root, targetSum);
        return res;
    }
};

路径总和3

题目链接
在这里插入图片描述

/**
 * 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 count = 0;
    int pathSum(TreeNode *root, int targetSum)
    {
        if (!root)
            return 0;
        dfs(root, targetSum);            //以root为起始点查找路径
        if(root->left) pathSum(root->left, targetSum);  //左子树递归
        if(root->right) pathSum(root->right, targetSum); //右子树递归
        return count;
    }
    void dfs(TreeNode *root, int sum)
    {
        sum -= root->val;
        if (sum == 0) //注意不要return,因为不要求到叶节点结束,所以一条路径下面还可能有另一条
            count++;  //如果找到了一个路径全局变量就+1
        if(root->left) dfs(root->left, sum);
        if(root->right) dfs(root->right, sum);
    }
};

二叉树最大路径和

题目链接
在这里插入图片描述

/**
 * 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 {
    int res = INT_MIN;
    int maxpath(TreeNode* root){
        if(!root) return 0;
        int left = max(maxpath(root->left), 0);
        int right = max(maxpath(root->right), 0);
        res = max(res, left + right + root->val);
        return max(left, right) + root->val;
    }
public:
    int maxPathSum(TreeNode* root) {
        maxpath(root);
        return res;
    }
};

二叉树最长相同路径

题目链接
在这里插入图片描述

/**
 * 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 {
    int res = 0;
    int longestPath(TreeNode* root){
        if(!root) return 0;
        int left = longestPath(root->left);
        int right = longestPath(root->right);
        if(root->left && root->val == root->left->val) left++;
        else left = 0;
        if(root->right && root->val == root->right->val) right++;
        else right = 0;
        res = max(res, left + right);
        return max(left, right);
    }
public:
    int longestUnivaluePath(TreeNode* root) {
        longestPath(root);
        return res;
    }
};

二叉树的直径

题目链接
在这里插入图片描述

/**
 * 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 res = 0;  
    int diameterOfBinaryTree(TreeNode *root)
    {
        maxPath(root);
        return res;
    }
    int maxPath(TreeNode *root)
    {
    // 这里递归结束条件要特别注意:不能是!root(而且不需要判断root为空,因为只有非空才会进入递归),因为单个节点路径长也是0
        if (!root->left && !root->right)  
            return 0;
        int left = root->left ? maxPath(root->left) + 1 : 0;  //判断左子节点是否为空,从而更新左边最长路径
        int right = root->right ? maxPath(root->right) + 1 : 0;
        res = max(res, left + right); //更新全局变量
        return max(left, right);  //返回左右路径较大者
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VioletEvergarden丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值