主要是回溯算法结合树的深度遍历
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int sumNumbers(TreeNode root) { if(root==null){ return 0; } List<TreeNode> root2Leaf = new ArrayList<TreeNode>(); root2Leaf.add(root); int[] sum = new int[1]; backTrack(root, root2Leaf, sum); return sum[0]; } private void backTrack(TreeNode node, List<TreeNode> root2Leaf, int[] sum){ if(node.left==null && node.right==null){ process(root2Leaf, sum); } TreeNode left = node.left; if(left != null){ int size = root2Leaf.size(); root2Leaf.add(left); backTrack(left, root2Leaf, sum); root2Leaf.remove(size); } TreeNode right = node.right; if(right != null){ int size = root2Leaf.size(); root2Leaf.add(right); backTrack(right, root2Leaf, sum); root2Leaf.remove(size); } } private void process(List<TreeNode> root2Leaf, int[] sum){ int d = 0; for(int i=0; i<root2Leaf.size(); i++){ d += (int)Math.pow(10, root2Leaf.size()-i-1) * root2Leaf.get(i).val; } sum[0] += d; } }
相关推荐
LeetCode题解 - Java语言实现-181页.pdf
public void helper(TreeNode root, int level){// 当前层没有 list,新建// 取得当前层的 list迭代pub
java面试 java面试_leetcode面试题解之最大数_java题解
LeetCode 刷题笔记 with Java 51-100(暗黑版).pdf
java笔试题-LeetCode 刷题笔记 with Java 1-50(暗黑版)
https://github.com/geekxingyun/leetcode-java-algorithm-solution.git 然后你会发现当前项目有两个模块。 完成:它是完成的项目,并有一些关于 leetcode 的答案。 初始:只需在模块项目中编写代码 如果您不知道...
leetcode题库所有数据库问题 Leetcode 所有数据库问题:Leetcode 问题 Active-Businesses-LeetCode.png 活跃用户-LeetCode.png 活动-参与者-LeetCode.png 至少合作过三次的演员和导演-LeetCode.png Ads-Performance-...
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 ...
LeetCode 刷题笔记 with Java 1-50(暗黑版).pdf
leetcode 和 oj 力码 一些基于 Java 的有趣的 OJ 实践。 主要来自 LeetCode,部分来自 LintCode 和 Google OJ。 细绳 - 添加二进制 大批 - 计数位-第三个最大数量 堆栈和堆 -解码字符串- 在每个树行中找到最大值- ...
Leetcode-Java- 一.(Sort类): 350. Intersection of Two Arrays II a.首先用HashMap遍历一遍数组nums1,Key值储存数组元素,Value(初始值为1)值储存重复元素出现次数,每出现一次加1; b.用List储存nums2中与nums...
leetcode添加元素使和等于 算法思想 双指针 双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。 有序数组的 Two Sum Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2 题目...
awesome-java-leetcode 我如今是一名 Android Developer,大学的我曾是一名 ACMer,我一直认为数据结构和算法是作为一名程序员必须掌握和善于利用的,为了不让数据结构和算法淡出我的记忆,所以我打算重拾 LeetCode ...
leetcode 答案LeetCode-答案-Java 我的 leetcode 用伪代码和分析用 java 回答
解压Leetcode-Solution-With-Java 用 Java 8 解决 Leetcode 问题 编号 问题 解决方案 困难 01 简单的 02 中等的 03 无重复字符的最长子串 中等的 04 两个有序数组的中位数 难的 653 二和 IV - 输入是 BST 简单的 ...
2019-12-07-二叉树深度遍历 Leetcode 107 2019-12-09-Leet232-栈实现队列出入栈 Leetcode 232 考察栈和队列 2019-12-11-Leetcode347-优先队列TopK Leetcode 347【难度中等】 考查二叉堆,优先队列 2019-12-12-全排列...
awesome-java-leetcode 我如今是一名 Android Developer,大学的我曾是一名 ACMer,我一直认为数据结构和算法是作为一名程序员必须掌握和善于利用的,为了不让数据结构和算法淡出我的记忆,所以我打算重拾 LeetCode ...
leetcode 2 和 c 938.-BST-C-Leetcode 的范围总和 给定二叉搜索树的根节点和两个整数 low 和 high,返回值在包含范围 [low, high] 内的所有节点的值之和。 示例 1: 输入:root = [10,5,15,3,7,null,18],低 = 7,高...
leetcode 不会LeetCode_628--三数的最大积 给定一个整数数组,找出乘积最大的三个数,并输出最大的乘积。 示例 1: 输入:[1,2,3] 输出:6 示例 2: 输入:[1,2,3,4] 输出:24 笔记: 给定数组的长度在 [3,104] 范围...
leetcode提交记录消失解决leetcode问题 日志 模板: - id : type : difficulty : easy url : first-submission-successful : yes 无法解决 2018-05-31 : - id : 13 type : string difficulty : easy url : ...