Last active
February 18, 2016 23:17
-
-
Save ademidun/acaa9523ed62e0ae8325 to your computer and use it in GitHub Desktop.
Week2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Inorder(node *root) { | |
| if (root) { | |
| Inorder(root->left); | |
| cout << to_string(root->data) + " "; | |
| Inorder(root->right); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Postorder(node *root) { | |
| if (root) { | |
| Postorder(root->left); | |
| Postorder(root->right); | |
| std::cout << to_string(root->data)+" "; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Preorder(node *root) { | |
| if (root) { | |
| std::cout << to_string(root->data)+" "; | |
| Preorder(root->left); | |
| Preorder(root->right); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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