Skip to content

Instantly share code, notes, and snippets.

@ademidun
Last active February 18, 2016 23:17
Show Gist options
  • Save ademidun/acaa9523ed62e0ae8325 to your computer and use it in GitHub Desktop.
Save ademidun/acaa9523ed62e0ae8325 to your computer and use it in GitHub Desktop.
Week2
void Inorder(node *root) {
if (root) {
Inorder(root->left);
cout << to_string(root->data) + " ";
Inorder(root->right);
}
}
void Postorder(node *root) {
if (root) {
Postorder(root->left);
Postorder(root->right);
std::cout << to_string(root->data)+" ";
}
}
void Preorder(node *root) {
if (root) {
std::cout << to_string(root->data)+" ";
Preorder(root->left);
Preorder(root->right);
}
}
class Solution {
public:
int sumNumbers(TreeNode * n)
{
int sum=0;
sumNumbers(n, 0, sum);
return sum;
}
void sumNumbers(TreeNode * n, int currNum, int &sum)
{
if (!n)
return;
currNum= currNum*10+n->val;
if (!n->left && !n->right)
sum += currNum;
if (n->left)
sumNumbers(n->left, currNum, sum);
if (n->right)
sumNumbers(n->right, currNum, sum);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment