【leetcode】Sum Root to leaf Numbers

简单的二叉树的先根遍历模板的应用

class Solution:
    # @param root, a tree node
    # @return an integer
    def hehe(self, num, root):
        #再原来的基础上*10,再加上当前的root.val
        num = num * 10 + root.val

        #是叶子节点了,则返回获得的路径值,通过这个判断,就保证了上一条语句
        #的root是不空的
        if None == root.left and None == root.right:
            return num

        #分别判断左右孩子
        left = 0
        if root.left:
            left = self.hehe(num, root.left)

        right = 0
        if root.right:
            right = self.hehe(num, root.right)

        #返回两部分的和值
        return right + left

    def sumNumbers(self, root):
        if None == root: return 0
        return self.hehe(0, root)
时间: 2024-08-04 01:47:29

【leetcode】Sum Root to leaf Numbers的相关文章

【LeetCode】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 /

【leetcode】Sum Root to Leaf Numbers(hard)

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 T

【LeetCode】 Sum Root to Leaf Numbers

第一次写的解法,.对于只有一个儿子的节点重复了两次 ..结果就弄复杂了..我也觉得不应该能有这么多的重复嘛 <span style="font-size:18px;">/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ package java

【leetcode刷题笔记】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 T

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

Leetcode dfs Sum Root to Leaf Numbers

Sum Root to Leaf Numbers Total Accepted: 20237 Total Submissions: 67979My Submissions 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

[C++]LeetCode: 94 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

Java for 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. 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 T