题目:

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 the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
/ \
2 3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

代码:

作为第一个做的Medium级别的题目,还是有点困难的。

关键两步都参考了百度:1. 递归的方式创建一颗二叉树(用来测试);2. 递归的方式深度优先遍历二叉树并求和。

1. 递归的方式创建一颗二叉树(用来测试):

递归方法简历二叉树,就不多说了,有兴趣去调试一下,一目了然。

public TreeNode(int[] array){
        root=makeBinaryTreeByArray(array,1);
    }

/**
     * 采用递归的方式创建一颗二叉树
     * 传入的是二叉树的数组表示法
     * 构造后是二叉树的二叉链表表示法
     */
    public static TreeNode makeBinaryTreeByArray(int[] array,int index){
//        System.out.print("递归建树数组剩余: "+index+array.length);
        if(index<array.length){
            int value=array[index];
//            if(array[index]!=0){
                TreeNode t=new TreeNode(value);
                array[index]=0;
                t.left=makeBinaryTreeByArray(array,index*2);
                t.right=makeBinaryTreeByArray(array,index*2+1);
                return t;
//            }
        }
        return null;
    }

2. 递归的方式深度优先遍历二叉树并求和:

从root节点开始,调用sumNumbers(),首先将root的值计入变量tmpsum。

如果左子树不为空,以左子树节点为参数,递归调用sumNumbers()函数,此时第一次记录root的值*10加上该节点的值。

左子树优先处理,所以会一直调用完,相当于深度优先遍历,组合出全部左树的结果。

左子树处理之后就回去遍历右子树,每次都是先左后右。

如果右子树不为空,同样以右子树节点为参数,递归调用sumNumbers()函数,此时第一次记录root的值*10加上该节点的值。

某个节点左右子树都未空时,说明当前已经是最下层节点了,于是*10+val生成本条路径的tmpsum,之后加入变量result中。

解释了这么多,肯定还是懵逼,自己去画一个二叉树,试一试就发现,是这个样子滴!

int result;
    public int sumNumbers(TreeNode root) {
        if(root==null){
            System.out.println("empty tree");
         return 0;
         }
        result =0 ;
        sumnuber(root,0);
        System.out.print(result);
        return result;
    }    
    public int sumnuber(TreeNode root,int tmpsum) {
         if(root.left == null && root.right == null)  
         {  
             result += tmpsum * 10 + root.val;  
//             System.out.print(result+" ");
         }  
         if(root.left != null)  
         {  
             sumnuber(root.left, tmpsum * 10 + root.val);
         }  
         if(root.right != null)  
         {  
             sumnuber(root.right, tmpsum * 10 + root.val);  
         }
         return result;
    }

129. Sum Root to Leaf Numbers的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  3. LeetCode OJ 129. Sum Root to Leaf Numbers

    题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...

  4. 129. Sum Root to Leaf Numbers pathsum路径求和

    [抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...

  5. [LeetCode] 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 ...

  6. [LC] 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 ...

  7. leetcode 129. Sum Root to Leaf Numbers ----- java

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

  9. [LeetCode] 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 ...

随机推荐

  1. HDOJ 1576 A/B

    数论水题... A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 第一节 HTML网页和CSS样式

    1. 第一行 <!DOCTYPE html> 表明网页使用的是HTML5版本 2. 网页的head内容,包含了 title,meta. 3. 网页的标题 title,注意这个显示在浏览器的 ...

  3. Java设计模式 之 工厂方法模式

    1. 使用设计模式的好处:可提高代码的重复性,让代码更容易被他人理解,保证代码的可靠性. 2. 工厂模式定义:就是创建一个工厂类来创建你需要的类,工厂模式包括工厂模式和抽象工厂模式,抽象工厂模式是工厂 ...

  4. RouterOS首次打开网页强制跳转

    网上极少有关于RouterOS的第一次打开网页强制跳转主页的方法,大多数都方法是将浏览某个域名的IP地址跳转到自己的主页,这种方法有时会失效.还有一种方法就是当用户用80端口连接时,抓取源地址到地址列 ...

  5. CodeIgniter框架下载辅助函数的一个小bug

    if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE) { header('Content-Type: '.$mime ...

  6. c语言随机函数&&时间函数

    c语言中的随机函数为rand(),但是rand生成的值得大小主要相对一个变量才产生的一定有含义的数,这个相对的变量我们可以再srand()函数中进行设置,srand函数是void类型,内部含一个无符号 ...

  7. ja

    import java.util.*; class animal{     void cry(){            }     void get_animal_name(){           ...

  8. curl模拟自动登陆&采集网页数据

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  9. 1.6---旋转二维数组,旋转图像像素,旋转矩阵,90度(CC150)

    import java.util.*; public class Transform { public int[][] transformImage(int[][] matrix, int n) { ...

  10. jquery数组内多维对象

    jquery数组内多维对象 var postData=[],obj,list; obj = !!obj ? obj : $('#dist_meici_checkinfo_form'); obj.fin ...