leetcode 666. Path Sum IV

If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers.

For each integer in this list:

  1. The hundreds digit represents the depth D of this node, 1 <= D <= 4.
  2. The tens digit represents the position P of this node in the level it belongs to, 1 <= P <= 8. The position is the same as that in a full binary tree.
  3. The units digit represents the value V of this node, 0 <= V <= 9.

 

Given a list of ascending three-digits integers representing a binary with the depth smaller than 5. You need to return the sum of all paths from the root towards the leaves.

Example 1:

Input: [113, 215, 221]
Output: 12
Explanation: 
The tree that the list represents is:
    3
   / \
  5   1

The path sum is (3 + 5) + (3 + 1) = 12.

 

Example 2:

Input: [113, 221]
Output: 4
Explanation: 
The tree that the list represents is: 
    3
     \
      1

The path sum is (3 + 1) = 4.

这到题目要求每个叶子结点到根结点的路径上的权值和。我有几个想法,今天写了第一种想法的代码:

第一种写法,每层用map记录节点在当前层位置和权值信息,然后根据孩子结点 = (父亲结点+1)/2来进行位置索引不断向下层进行权值相加,直到叶子结点。具体看代码

class Solution {
public:
    int pathSum(vector<int>& nums) {
        vector<map<int,int> > v;
        int cnt = 1;
        map<int, int> mp;
        for (int i = 0; i < nums.size(); ++i) {
            int x = nums[i]/100;
            if (x == cnt) {
                mp[(nums[i]/10)%10] = nums[i]%10;
            } else {
                v.emplace_back(mp);
                cnt ++;
                mp.clear();
                mp[(nums[i]/10)%10] = nums[i]%10;
            }
        }
        v.emplace_back(mp);
        int sum = 0;
        for (int i = 1; i < v.size(); ++i) {
            map<int, int> tmp = v[i-1];
            for (auto &x :v[i]) {
                x.second += tmp[(x.first+1)/2];
            }
        }
        for (int i = 0; i < v.size(); ++i) {
            if (i == v.size() - 1) {
                for (auto x: v[i]) {
                    sum += x.second;
                }
            } else {
                map<int,int> tmp = v[i+1];
                for (auto x: v[i]) {
                    if (!tmp[x.first*2] && !tmp[x.first*2-1])
                        sum += x.second;
                }
            }
        }
        return sum;
    }
};

第二种方法:

....就是简化上面的代码,用一个数组代替map  同时不用把全部的结点信息存储,用两个数组滚动

这是其他人的代码

class Solution {
public:
    int pathSum(vector<int>& nums) {
        if (nums.empty()) return 0;
        vector<int> tree(8, 0);
        int dep = nums.back()/100, n = nums.size(), ans = 0;
        for (int i = 1, j = 0; i <= dep; i++) {
            vector<int> tmp(8, 0);
            // fill path sum in nodes of each level
            while (j < n && nums[j]/100 == i) {
                int pos = nums[j]%100/10-1;
                tmp[pos] = nums[j++]%10 + tree[pos/2];
            }
            // if both children are null, add the parent leaf node
            // also work for node value of 0, such as 210
            for (int k = 0; k < 4; k++) {
                if (tmp[k*2] == 0 && tmp[k*2+1] == 0)
                    ans += tree[k];
            }
            swap(tree, tmp);
        }
        return accumulate(tree.begin(), tree.end(), ans);
    }
};

 

posted on 2017-08-28 21:32  Beserious  阅读(363)  评论(0编辑  收藏  举报