目录
一、题目
二、代码
一、题目
二、代码
/**
* 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 PreOrder(TreeNode*root,int sum)
{
if(root == nullptr)
{
return 0;
}
sum=sum<<1 |root->val;
if(root->left==nullptr && root->right==nullptr)
{
return sum;
}
return PreOrder(root->left,sum)+PreOrder(root->right,sum);
}
int sumRootToLeaf(TreeNode* root) {
int sum=0;
return PreOrder(root,sum);
}
};