题目链接
int sumNumbers(TreeNode *root) {
vector<TreeNode*> nodes;
vector<int> numbers;
int sum_numbers = 0;
if(root != NULL)
{
nodes.push_back(root);
numbers.push_back(root->val);
}
while(!nodes.empty())
{
TreeNode* cur_node = nodes.back();
int cur_number = numbers.back();
TreeNode* left_child = cur_node->left;
TreeNode* right_child = cur_node->right;
nodes.pop_back();
numbers.pop_back();
if(right_child != NULL)
{
nodes.push_back(right_child);
int right_number = cur_number*10 + right_child->val;
numbers.push_back(right_number);
}
if(left_child != NULL)
{
nodes.push_back(left_child);
int left_number = cur_number*10 + left_child->val;
numbers.push_back(left_number);
}
if(left_child == NULL && right_child == NULL)
{
sum_numbers += cur_number;
}
}
return sum_numbers;
}
参考Binary Tree Postorder Traversal遍历方法