LeetCode 题解(43): Sum Root to Leaf Numbers

题目:

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 number123.

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.


题解:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 using namespace std;
 
class Solution {
public:
    int sum = 0;
    int sumNumbers(TreeNode *root) {
        if(!root)
            return 0;
        vector<int> temp;
        temp.push_back(root->val);
        traverse(root, temp);
        return sum;
    }
    
    void traverse(TreeNode *node, vector<int> &temp) {
        if(!node->left && !node->right) {
            int current = 0;
            for(vector<int>::iterator i = temp.begin(); i != temp.end(); i++) {
                current = current * 10 + *i;
            }
            sum += current;
            temp.pop_back();
            return;
        }
        if(node->left) {
            temp.push_back(node->left->val);
            traverse(node->left, temp);
        }
        if(node->right) {
            temp.push_back(node->right->val);
            traverse(node->right, temp);
        }
        temp.pop_back();
    }
};

改进一下:

class Solution {
public:
    int sum = 0;
    int sumNumbers(TreeNode *root) {
        if(!root)
            return 0;
        int temp = 0;
        traverse(root, temp);
        return sum;
    }
    
    void traverse(TreeNode *node, int temp) {
        if(!node->left && !node->right) {
            sum += node->val + temp * 10;
            return;
        }
        if(node->left) {
            traverse(node->left, temp * 10 + node->val);
        }
        if(node->right) {
            traverse(node->right, temp * 10 + node->val);
        }
    }
};

Java版:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int sum;
    public int sumNumbers(TreeNode root) {
        int temp = 0;
        traverse(root, temp);
        return sum;
    }
    public void traverse(TreeNode root, int temp) {
        if(root == null)
            return;
        if(root.left == null && root.right == null){
            sum += root.val + temp * 10;
            return;
        }
        if(root.left != null) {
            traverse(root.left, temp * 10 + root.val);
        }
        if(root.right != null) {
            traverse(root.right, temp * 10 + root.val);
        }
    }
}

Python版:

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return an integer
    def __init__(self):
        self.sum = 0
    
    def traverse(self, root, temp):
        if root == None:
            return
        if root.left == None and root.right == None:
            self.sum += root.val + temp * 10
            return
        if root.left != None:
            self.traverse(root.left, temp * 10 + root.val)
        if root.right != None:
            self.traverse(root.right, temp * 10 + root.val)
        
    def sumNumbers(self, root):
        temp = 0
        self.traverse(root, temp)
        return self.sum


你可能感兴趣的:(Algorithm,LeetCode,tree,traverse)