# 437. Path Sum III (Easy)
https://leetcode.com/problems/path-sum-iii/
/** | |
* 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: | |
long pathSum(TreeNode *root, int targetSum) { | |
return root ? startSumWithRoot(root, targetSum) + pathSum(root->right, targetSum) + | |
pathSum(root->left, targetSum) : 0; | |
} | |
long startSumWithRoot(TreeNode *root, long targetSum) { | |
// 递归终点 | |
if (!root) { | |
return 0; | |
} | |
long count = 0; | |
if(targetSum == root->val) { | |
count = 1; | |
} | |
count += startSumWithRoot(root->left, targetSum - root->val); | |
count += startSumWithRoot(root->right, targetSum - root->val); | |
return count; | |
} | |
}; |
# 101. Symmetric Tree (Easy)
https://leetcode.com/problems/symmetric-tree/
/** | |
* 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: | |
bool isSymmetric(TreeNode* root) { | |
return isSymmetric(root->left, root->right); | |
} | |
bool isSymmetric(TreeNode* left, TreeNode* right) { | |
if(!left && !right) { | |
return true; | |
}else if(!left || !right) { | |
return false; | |
}else{ | |
if(left->val == right->val) { | |
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left); | |
}else{ | |
return false; | |
} | |
} | |
} | |
}; |