题目
给你一个二叉树的根节点 root ,树中每个节点都存放有一个 0 到 9 之间的数字。
每条从根节点到叶节点的路径都代表一个数字:
例如,从根节点到叶节点的路径 1 -> 2 -> 3 表示数字 123 。
计算从根节点到叶节点生成的 所有数字之和 。
叶节点 是指没有子节点的节点。
解法递归:
public int getCount(TreeNode<Integer> root,int count) {if(root==null){return 0;}int c=count*10+root.getData();if(root.lTreeNode==null && root.rTreeNode==null){return c;}int count2 = getCount(root.lTreeNode, c);int count3=getCount(root.rTreeNode,c);return count2+count3;}//调用getCount(root,0);
