二叉树的遍历
代码:
1 vector<int> path; 2 3 int sumNumbers(TreeNode *root) { 4 if (!root) 5 return 0; 6 7 int sum = 0; 8 path.push_back(root->val); 9 10 if (!root->left && !root->right) { 11 for (int i = 0; i < path.size(); i++) 12 sum = sum * 10 + path[i]; 13 } 14 else { 15 if (root->left) 16 sum += sumNumbers(root->left); 17 if (root->right) 18 sum += sumNumbers(root->right); 19 } 20 21 path.pop_back(); 22 23 return sum; 24 }