算法分析与设计课程作业第三周#1

算法分析与设计课程作业第三周#1

这周先挑选了一道有关深度优先搜索的medium题来做,废话不多说,上题:

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

思路:

就是深度优先遍历该树,可以使用递归的方法,关键是递归什么时候结束,递归结束时要做什么,以及每次递归要传递什么信息。递归到一个节点的两个子节点都为空时结束(即该节点为叶节点),此时可以将到叶节点的路径生成的数放进一个vector里,也可直接加到一个sum上,而每次递归都将到本节点路径形成的数传递到下一递归,下一递归将传递的数乘以10加上下一递归对应节点的数得到由根节点到下一递归对应节点路径形成的数。

代码块

第一次错误:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void traverse(TreeNode* root, int value, vector<int> &nums){
        if(root == NULL){
            nums.push_back(value);
        }
        else{
            traverse(root->left, 10 * value + root->val, nums);
            traverse(root->right, 10 * value + root->val, nums);
        }
    }
    int sumNumbers(TreeNode* root) {
        vector<int> nums;
        traverse(root, 0, nums);
        int sum = 0;
        vector<int>::iterator it = nums.begin();
        while(it != nums.end()){
            sum += *it;
            it++;
        }
        return sum;
    }
};

这个错误的原因是:这样每个叶节点对应路径形成的数都会加上两次(因为叶节点的两个子节点都为空),而每个有一个子节点为空的节点对应路径形成的数也会被加进去。
第二次错误:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void traverse(TreeNode* root, int value, vector<int> &nums){
        if(root->left == NULL && root->right == NULL){
            nums.push_back(10 * value + root->val);
        }
        else{
            if(root->left != NULL) traverse(root->left, 10 * value + root->val, nums);
            if(root->right != NULL)traverse(root->right, 10 * value + root->val, nums);
        }
    }
    int sumNumbers(TreeNode* root) {
        vector<int> nums;
        traverse(root, 0, nums);
        int sum = 0;
        vector<int>::iterator it = nums.begin();
        while(it != nums.end()){
            sum += *it;
            it++;
        }
        return sum;
    }
};

错误的原因:没考虑空树。
判断根节点是否为空再进行递归即可:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void traverse(TreeNode* root, int value, vector<int> &nums){
        if(root->left == NULL && root->right == NULL){
            nums.push_back(10 * value + root->val);
        }
        else{
            if(root->left != NULL) traverse(root->left, 10 * value + root->val, nums);
            if(root->right != NULL)traverse(root->right, 10 * value + root->val, nums);
        }
    }
    int sumNumbers(TreeNode* root) {
        vector<int> nums;
        if(root != NULL) traverse(root, 0, nums);
        int sum = 0;
        vector<int>::iterator it = nums.begin();
        while(it != nums.end()){
            sum += *it;
            it++;
        }
        return sum;
    }
};

或者不使用vector也可:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void traverse(TreeNode* root, int value, int& sum){
        if(root->left == NULL && root->right == NULL){
            sum += 10 * value + root->val;
        }
        else{
            if(root->left != NULL) traverse(root->left, 10 * value + root->val, sum);
            if(root->right != NULL)traverse(root->right, 10 * value + root->val, sum);
        }
    }
    int sumNumbers(TreeNode* root) {
        int sum = 0;
        if(root != NULL) traverse(root, 0, sum);
        return sum;
    }
};

你可能感兴趣的:(算法分析与设计)