其实本题并不难,没有解出主要是题干没有写清楚,不知道是level还是height,
    后来搞明白[[b], a, [c, d]],在这个例子中,b是在level 2的,而不是level 1,即便他是leaf node。
    这样就是一个很简单的用BFS的问题。
    代码如下:

    1. /**
    2. * // This is the interface that allows for creating nested lists.
    3. * // You should not implement it, or speculate about its implementation
    4. * public interface NestedInteger {
    5. * // Constructor initializes an empty nested list.
    6. * public NestedInteger();
    7. *
    8. * // Constructor initializes a single integer.
    9. * public NestedInteger(int value);
    10. *
    11. * // @return true if this NestedInteger holds a single integer, rather than a nested list.
    12. * public boolean isInteger();
    13. *
    14. * // @return the single integer that this NestedInteger holds, if it holds a single integer
    15. * // Return null if this NestedInteger holds a nested list
    16. * public Integer getInteger();
    17. *
    18. * // Set this NestedInteger to hold a single integer.
    19. * public void setInteger(int value);
    20. *
    21. * // Set this NestedInteger to hold a nested list and adds a nested integer to it.
    22. * public void add(NestedInteger ni);
    23. *
    24. * // @return the nested list that this NestedInteger holds, if it holds a nested list
    25. * // Return null if this NestedInteger holds a single integer
    26. * public List<NestedInteger> getList();
    27. * }
    28. */
    29. class Solution {
    30. public int depthSumInverse(List<NestedInteger> nestedList) {
    31. if (nestedList == null || nestedList.isEmpty()) {
    32. return 0;
    33. }
    34. Queue<NestedInteger> queue = new LinkedList<>();
    35. queue.addAll(nestedList);
    36. int preSum = 0;
    37. int sum = 0;
    38. while (!queue.isEmpty()) {
    39. int levelSum = 0;
    40. int sz = queue.size();
    41. for (int i = 0; i < sz; ++i) {
    42. NestedInteger ni = queue.poll();
    43. if (ni.isInteger()) {
    44. levelSum += ni.getInteger();
    45. }
    46. else {
    47. queue.addAll(ni.getList());
    48. }
    49. }
    50. preSum += levelSum;
    51. sum += preSum;
    52. }
    53. return sum;
    54. }
    55. }