[leetcode/lintcode 题解] 阿里巴巴面试题: 路径和 IV

简介: [leetcode/lintcode 题解] 阿里巴巴面试题: 路径和 IV

描述
如果树的深度小于5,则该树可以由三位整数的列表表示。
对于此列表中的每个整数:
百位数表示该节点的深度D,1 <= D <= 4。
2.十位数表示该节点在其所属级别中的位置P,1 <= P <= 8.该位置与完整二叉树中的位置相同。
3.单位数字表示该节点的值V,0 <= V <= 9。
给定一个表示深度小于5的二叉树的升序三位整数列表,您需要返回从根到叶子的所有路径和的总和。

在线评测地址:领扣题库官网

样例 1:
输入: [113, 215, 221]
输出: 12
解释: 
该树如下图所示:
    3
   / \
  5   1

所有的路径和为 (3 + 5) + (3 + 1) = 12.
样例 2:
输入: [113, 221]
输出: 4
解释: 
该树如下所示: 
    3
     \
      1

所有的路径和为 (3 + 1) = 4.

解题思路
先将每个数的前两位取出,还原其在二叉树对应数组的位置,将其值放入对应数组的位置中。再按深度搜索的方法计算所有路径的和,并相加得出结果

源代码

public class Solution {
    /**
     * @param nums: the list
     * @return: the sum of all paths from the root towards the leaves
     */
    class Pair {
        int pathSum;
        int index;

        Pair(int pathSum, int index) {
            this.pathSum = pathSum;
            this.index = index;
        }
    }

    public int pathSumIV(int[] nums) {
        int[] nodes = new int[16];
        Arrays.fill(nodes, -1);
        for (int num : nums) {
            int units = num % 10;
            int tens = num / 10 % 10;
            int hundreds = num / 100 % 10;
            int index = (int) Math.pow(2, hundreds - 1) + (tens - 1);
            nodes[index] = units;
        }
        Pair root = new Pair(nodes[1], 1);
        Stack<Pair> stack = new Stack<>();
        stack.push(root);
        int sum = 0;
        while (!stack.isEmpty()) {
            Pair node = stack.pop();
            int index = node.index;
                        // Reach a leaf node. This path ends.
            if ((index * 2 >= 16 && index * 2 + 1 >= 16) || (nodes[index * 2] == -1 && nodes[index * 2 + 1] == -1)) {
                sum += node.pathSum;
            }
            if (index * 2 < 16 && nodes[index * 2] != -1) {
                stack.push(new Pair(node.pathSum + nodes[index * 2],
                        index * 2));
            }
            if (index * 2 + 1 < 16 && nodes[index * 2 + 1] != -1) {
                stack.push(new Pair(node.pathSum + nodes[index * 2 + 1],
                        index * 2 + 1));
            }
        }
        return sum;
    }
}

更多题解参考:九章官网solution

相关文章
|
14天前
|
机器人 Python
【Leetcode刷题Python】62. 不同路径
LeetCode 62题 "不同路径" 的Python解决方案,使用动态规划算法计算机器人从网格左上角到右下角的所有可能路径数量。
28 0
|
4天前
|
存储 算法 Linux
LeetCode第71题简化路径
文章讲述了LeetCode第71题"简化路径"的解题方法,利用栈的数据结构特性来处理路径中的"."和"..",实现路径的简化。
LeetCode第71题简化路径
|
4天前
|
算法
LeetCode第64题最小路径和
LeetCode第64题"最小路径和"的解题方法,运用动态规划思想,通过构建一个dp数组来记录到达每个点的最小路径和,从而高效求解。
LeetCode第64题最小路径和
|
14天前
|
算法 JavaScript Python
【Leetcode刷题Python】79. 单词搜索和剑指 Offer 12. 矩阵中的路径
Leetcode第79题"单词搜索"的Python解决方案,使用回溯算法在给定的二维字符网格中搜索单词,判断单词是否存在于网格中。
18 4
|
14天前
|
存储 Python
【Leetcode刷题Python】滑雪路径消耗时间:Testing Round #16 (Unrated) C. Skier
Leetcode题目"Testing Round #16 (Unrated) C. Skier"的Python解决方案,题目要求计算给定滑雪路径字符串的总耗时,其中未走过的边耗时5秒,走过的边耗时1秒。
26 4
|
14天前
|
存储 Python
【Leetcode刷题Python】1496.判断路径是否相交
Leetcode第1496题"判断路径是否相交"的Python代码实现,通过使用字典存储方向和集合记录访问过的坐标点来检测路径是否与自身相交。
27 2
|
14天前
|
机器人 Python
【Leetcode刷题Python】63. 不同路径 II
LeetCode 63题 "不同路径 II" 的Python解决方案,使用动态规划算法计算在有障碍物的网格中从左上角到右下角的不同路径数量。
15 1
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
27 6
|
13天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
42 2
|
13天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
16 7