二叉树所有路径
/**
* 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); //返回左右路径较大者
}
};