leetcode 129. Find the sum of numbers from root to leaf nodes

Given a binary tree, each node of it stores a  0-9 number, and each path from the root to the leaf node represents a number.

For example, paths from root to leaf nodes  1->2->3 represent numbers  123.

Calculate the sum of all numbers generated from root to leaf nodes.

Explanation: A leaf node is a node that has no child nodes.

Example 1:

Input: [1,2,3]
    1
   / \
  2   3
Output: 25
explain:
Paths from root to leaf nodes 1->2represent numbers 12.
Paths from root to leaf nodes 1->3represent numbers 13.
So sum of numbers = 12 + 13 = 25.

Example 2:

Input: [4,9,0,5,1]
    4
   / \
  9   0
 / \
5   1
Output: 1026
explain:
The path from the root to the leaf node 4->9->5represents the number 495.
The path from the root to the leaf node 4->9->1represents the number 491.
The path from the root to the leaf node 4->0represents the number 40.
So sum of numbers = 495 + 491 + 40 = 1026. 


Ideas:
  • Each path of the tree is accessed by pushing the stack, and each time a node is pushed, the value of the node is added to t
  • Use Boolean quantities fl, fr to indicate whether the left and right nodes are visited
  • Use sum to store the value of all paths, and t to represent the value of each path
  • When visiting a leaf node:
  • When both fl and fr are true, it means that the two child nodes of the node have been traversed, pop it up, and divide the t value by 10
  • When only one of fl and fr is false, it means that a path ends, add the t value to the sum, divide the t value by 10, and invert fr or fl

Why divide by 10? Because the current value pops up, the number of digits is reduced, which is equivalent to the lowest digit, so divide by 10

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 #include<stack>
11 #include<vector>
12 class Solution {
13 public:
14     vector<int> s;
15     vector<TreeNode*> v;
16     int sumNumbers(TreeNode* root) {
17         if(root == NULL) return 0;
18         int sum = 0, t = root->val;
19         stack<TreeNode*> s;
20         s.push(root);
21         if(root->left==NULL && root->right==NULL)return t;
22         bool fl = true, fr = true;
23         while(!s.empty()){
24             TreeNode* temp = s.top();
25             if(temp == NULL) break;
26             if(temp->left) {//遍历每一条路径
27                 s.push(temp->left);
28                 t = t*10 + temp->left->val;
29                 temp->left = NULL;
30                 fl = false;
31                 fr = true;
32             }else if(temp->right){
33                 s.push(temp->right);
34                 t = t*10 + temp->right->val;
35                 temp->right = NULL;
 36                  fl = true ;
 37                  fr = false ;
 38              } else {//traverse a complete path
 39                  if (! fl){
 40                      sum += t;
 41                      t /= 10 ;
 42                      fl = true ;
 43                  }
 44                  else  if (! fr){
 45                      sum += t;
 46                     t /= 10;
47                     fr = true;
48                 }
49                 else if(fl&&fr){
50                     t/=10;
51                 }
52                 s.pop();
53             }
54         }
55         return sum;
56     }
57 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023703&siteId=291194637