NC5. 二叉树根节点到叶子节点的所有路径和
描述
示例1
输入:
{1,2,3}
输出:
25
示例2
输入:
{1,0}
输出:
10
示例3
输入:
{1,2,0,3,4}
输出:
257
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-04-03
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sumNumbers(TreeNode *root) { int result=0; sumNumbers2(root, result); return result; } void sumNumbers2(TreeNode *root, int &result) { if(root == NULL) { return; } path.push_back(root->val); if(root->left==NULL && root->right==NULL) { int sum=0; for(vector<int>::iterator it=path.begin();it!=path.end();it++) { sum=sum*10+*it; } result+=sum; } if(root->left!=NULL) { sumNumbers2(root->left, result); } if(root->right!=NULL) { sumNumbers2(root->right, result); } path.pop_back(); } private: vector<int> path; };
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-04-03
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sumNumbers(TreeNode *root) { if (root == NULL) return 0; if ((root->left == NULL) && (root->right == NULL)) return root->val; stack <TreeNode *> pstack; stack <int> vstack; TreeNode *ptr = root; int sum = 0, temp = 0; int flag; while(ptr){ flag = 0; temp = temp * 10 + ptr->val; if(ptr->right != NULL){ pstack.push(ptr->right); vstack.push(temp); flag = 1; } if(ptr->left != NULL){ ptr = ptr->left; } else{ if(!flag){ sum += temp; } if(!pstack.empty()){ ptr = pstack.top(); pstack.pop(); temp = vstack.top(); vstack.pop(); } else break; } } //delete ptr; return sum; } };
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-03-28
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sumNumbers(TreeNode *root) { if (root == NULL) return 0; return calc(root, 0); } int calc(TreeNode* root, int curVal) { //if is leaf if (root->left == NULL && root->right == NULL) return curVal * 10 + root->val; int leftVal = 0, rightVal = 0; if (root->left != NULL) leftVal = calc(root->left, curVal * 10 + root->val); if (root->right != NULL) rightVal = calc(root->right, curVal * 10 + root->val); return leftVal + rightVal; } };
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-03-27
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sumNumbers(TreeNode *root) { int sum = 0; int number = 0; Total(root, number, sum); return sum; } void Total(TreeNode *root, int number,int &sum) { if (root == NULL) { sum += number; return; } number = number * 10 + root->val; if (root->left == NULL&&root->right == NULL) { sum += number; return; } if (root->left != NULL) Total(root->left, number, sum); if (root->right != NULL) Total(root->right, number, sum); return; } };
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-03-11
class Solution { public: int sumNumbers(TreeNode *root) { return dfs( root, 0); } int dfs(TreeNode *root, int sum) { if ( root == nullptr ) return 0; if ( root->left == nullptr && root->right == nullptr ) return sum * 10 + root->val; return dfs(root->left, sum * 10 + root->val) + dfs(root->right, sum * 10 + root->val); } };