Sum Root to Leaf Numbers

Q:

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.

A:

 厌恶了dfs的方式,用bfs的方式搞了一下,竟然一次通过,编译错误也没。ok

/**                                                                                                                   
 * 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) {                                                                                    
    // Start typing your C/C++ solution below                                                                         
    // DO NOT write int main() function                                                                               
    if (!root) return 0;                                                                                              
    int result = 0;                                                                                                   
    queue<TreeNode*> node_q;                                                                                          
    vector<int> father_nums;                                                                                          
    int handled_node_num = 1;                                                                                         
    node_q.push(root);
    father_nums.push_back(0);
                                                                                                                      
    while (!node_q.empty()) {                                                                                         
      int next_handle_node_num = 0;                                                                                   
      vector<int> next_nums;                                                                                          
      for (int i = 0; i < handled_node_num; ++i) {                                                                    
        TreeNode* node = node_q.front();                                                                              
        node_q.pop();
        int tmp_num = father_nums[i] * 10 + node->val;                                                                
        if (!node->left && !node->right) {                                                                            
          result += tmp_num;                                                                                          
          continue;                                                                                                   
        }                                                                                                             
        if (node->left) {                                                                                             
          node_q.push(node->left);
          next_nums.push_back(tmp_num);
          next_handle_node_num++;                                                                                     
        }                                                                                                             
        if (node->right) {                                                                                            
          node_q.push(node->right);
          next_nums.push_back(tmp_num);
          next_handle_node_num++;                           
        }                                                                                  
      }               
      handled_node_num = next_handle_node_num;        
      father_nums.swap(next_nums);    
    }                                                          
    return result;                          
  }                                              
};

 

posted @ 2013-07-02 21:36  dmthinker  阅读(113)  评论(0)    收藏  举报