题解 | #二叉树根节点到叶子节点的所有路径和#
二叉树根节点到叶子节点的所有路径和
https://www.nowcoder.com/practice/185a87cd29eb42049132aed873273e83
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int sumNumbers(TreeNode* root,int x=0) { if(root==nullptr){ return 0; } int ans=0; x+=root->val; if(root->left==nullptr&&root->right==nullptr){ return x; } ans+=sumNumbers(root->left,x*10); ans+=sumNumbers(root->right,x*10); return ans; } };