zoukankan      html  css  js  c++  java
  • 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 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.

    Hide Tags

    TreeDepth-first Search

    链接:  http://leetcode.com/problems/sum-root-to-leaf-numbers/

    题解:

    求二叉树路径和。依然是DFS,leaf的左右子节点均为空。

    Time Complexity - O(n), Space Complexity - O(n)。                 -要看一下递归和尾递归的空间复杂度

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int sumNumbers(TreeNode root) {
            return sumNumbers(root, 0);
        }
        
        private int sumNumbers(TreeNode root, int sum) {
            if(root == null)
                return 0;
            sum = sum * 10 + root.val;
            if(root.left == null && root.right == null)
                return sum;
            return sumNumbers(root.left, sum) + sumNumbers(root.right, sum);
        }
    }

    Update:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int sumNumbers(TreeNode root) {
            return sumNumbers(root, 0);
        }
        
        private int sumNumbers(TreeNode root, int num) {
            if(root == null)
                return 0;
            int sum = num * 10 + root.val;
            if(root.left == null && root.right == null)
                return sum;
            return sumNumbers(root.left, sum) + sumNumbers(root.right, sum);
        }
    }

    二刷:

    Java:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int sumNumbers(TreeNode root) {
            return sumNumbers(root, 0);
        }
        
        private int sumNumbers(TreeNode root, int sum) {
            if (root == null) return 0;
            sum = sum * 10 + root.val;
            if (root.left == null && root.right == null) return sum;
            else return sumNumbers(root.left, sum) + sumNumbers(root.right, sum); 
        }
    }

    Reference:

    https://leetcode.com/discuss/20451/short-java-solution-recursion

  • 相关阅读:
    ExtJs学习准备工作(二) firebug firefox插件的安装 全新时代
    Hibernate系统中调试SQL方式 全新时代
    Eclipse工程出现红叉导致无法编译的问题 全新时代
    javascript 取table中内容
    Asp.Net中清空所有textbox的几种方法
    SQL Server:使用系统存储过程实现的通用分页存储过程
    C# 检查网络是否连通
    sq分页原理
    SQL Server:日志备份和差异备份还原中的常见问题示例
    javascript:连接数据库
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4438737.html
Copyright © 2011-2022 走看看