package question;import java.util.LinkedList;import java.util.List;/*@author: Roderland@create: 2020年09月16日---18:21*/public class Question129 {public static void main(String[] args) {//[4,9,0,5,1]TreeNode t1 = new TreeNode(4);TreeNode t2 = new TreeNode(9);TreeNode t3 = new TreeNode(0);TreeNode t4 = new TreeNode(5);TreeNode t5 = new TreeNode(1);t1.left=t2;t1.right=t3;t2.left=t4;t2.right=t5;System.out.println(new Question129().sumNumbers(t1));}List<Integer> list;public int sumNumbers(TreeNode root) {if (root==null) return 0;list = new LinkedList<>();fun(root, root.val);int sum=0;for (Integer integer : list) {sum+=integer;}return sum;}public void fun(TreeNode root, int sum) {if (root.left==null&&root.right==null) list.add(sum);if (root.left!=null) {int left=sum*10+root.left.val;fun(root.left, left);}if (root.right!=null) {int right=sum*10+root.right.val;fun(root.right, right);}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。