题目

给你一个二叉树的根节点 root ,树中每个节点都存放有一个 0 到 9 之间的数字。
每条从根节点到叶节点的路径都代表一个数字:

例如,从根节点到叶节点的路径 1 -> 2 -> 3 表示数字 123 。
计算从根节点到叶节点生成的 所有数字之和 。

叶节点 是指没有子节点的节点。
解法递归:

  1. public int getCount(TreeNode<Integer> root,int count) {
  2. if(root==null){
  3. return 0;
  4. }
  5. int c=count*10+root.getData();
  6. if(root.lTreeNode==null && root.rTreeNode==null){
  7. return c;
  8. }
  9. int count2 = getCount(root.lTreeNode, c);
  10. int count3=getCount(root.rTreeNode,c);
  11. return count2+count3;
  12. }
  13. //调用
  14. getCount(root,0);