标签:
/**
* ID: 129
* Name: Sum Root to Leaf Numbers
* Data Structure:
* Time Complexity:
* Space Complexity:
* Tag: Tree
* Difficult: Medium
* Algorithm: DFS recursion
* Problem:
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
* Solution:
* 1. 想好左右子树 还有返回值
* What to learn:
*
* Note: to ask the interviewer the number maybe too large that the int maybe invalid.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
**/
1 #include <iostream> 2 #include <list> 3 #include <queue> 4 using namespace std; 5 6 7 struct TreeNode { 8 int val; 9 TreeNode *left; 10 TreeNode *right; 11 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 }; 13 14 struct ListNode { 15 int val; 16 ListNode *next; 17 ListNode(int x) : val(x), next(NULL){} 18 }; 19 20 21 22 ListNode* create_list(int* array,int n) 23 { 24 if (n==0) 25 return NULL; 26 ListNode *head,*current; 27 head = current = new ListNode(array[0]); 28 for (int i=1;i<n;i++) 29 { 30 ListNode *node = new ListNode(array[i]); 31 current -> next = node; 32 current = current -> next; 33 } 34 return head; 35 } 36 37 TreeNode *constructTree(string *dat , int len) 38 { 39 if(dat == NULL) 40 return NULL; 41 TreeNode *root = NULL; 42 int index = 0; 43 if(len > 0) 44 root = new TreeNode(stoi(dat[index])); 45 else 46 return NULL; 47 list<TreeNode *> node; //其实相当于 queue 48 node.push_back(root); 49 index ++; 50 while(index < len) 51 { 52 if(!node.empty()) 53 { 54 TreeNode *root = node.front(); 55 if(index < len ) 56 { 57 if(dat[index].compare("#") != 0) 58 { 59 root->left = new TreeNode(stoi(dat[index])); 60 node.push_back(root->left); 61 } 62 index ++; 63 } 64 if(index < len ) 65 { 66 if(dat[index].compare("#") != 0) 67 { 68 root->right = new TreeNode(stoi(dat[index])); 69 node.push_back(root->right); 70 } 71 index ++; 72 } 73 node.pop_front(); 74 } 75 } 76 return root; 77 } 78 79 void print(TreeNode * root) 80 { 81 if(root == NULL) 82 return ; 83 queue <TreeNode *> q; 84 q.push(root); 85 while(!q.empty()) 86 { 87 TreeNode *cur = q.front(); 88 cout<< cur->val; 89 q.pop(); 90 if(cur -> left) q.push(cur->left); 91 if(cur -> right) q.push(cur->right); 92 } 93 } 94 95 96 class Solution { 97 public: 98 int sumNumbers(TreeNode* root) { // DFS 想好递归的参数和返回值 99 if (root == NULL) 100 return 0; 101 if (root -> left == NULL && root -> right == NULL) 102 return root -> val; 103 int total = root -> val; 104 return dfs(root, 0) ; 105 } 106 int dfs(TreeNode* root, int sum) 107 { 108 if(root->left == NULL && root->right == NULL) 109 { 110 return sum*10+root->val; 111 } 112 113 else if (root ->left != NULL && root -> right != NULL) 114 return dfs(root->left, root->val + sum*10) + dfs(root->right, root->val + sum*10); 115 else if (root -> right != NULL) 116 return dfs(root->right, root->val + sum*10); 117 else if (root -> left != NULL) 118 return dfs(root->left, root->val + sum*10); 119 } 120 }; 121 122 int main() 123 { 124 //vector <int> a = {1,3,5}; 125 Solution ans; 126 //string a[13] = {"5","4","8","11","#","13","4","7","1","#","#","#","1"}; 127 string a[5] = {"1","0","#","#","#"}; 128 129 TreeNode * tree = constructTree(a,5); 130 int result = ans.sumNumbers(tree); 131 cout<<result; 132 133 //for () 134 //ListNode * pure = ans.addTwoNumbers(first,second); 135 //if(pure == NULL) 136 // printf("NULL"); 137 //else 138 //print(pure); 139 return 0; 140 }
Leetcode 129 Sum Root to Leaf Numbers
标签:
原文地址:http://www.cnblogs.com/zhuguanyu33/p/4550793.html