【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 = None

#         self.right = None

class
Solution:

    # @param root, a tree node

    # @return an integer

    def
sumNumbers(self, root):

        """

        BFS the tree from the root

        track each path as a root->leaf number

        """

        # specail cases

        if
root is
None:

            return
0

        # queue of the numbers

        # BSF the tree

        q =
[ (root, 0) ]

        res =
0

        while
q:

            new_q =
[]

            # expand all paths in q by one step

            for
(node, number) in
q:

                node_number =
number *
10 +
node.val

                # If the node is a leaf, remove this path and record the root-leaf number

                if
node.left ==
node.right ==
None:

                    res +=
node_number

                # Expand the path by left/right children

                else:

                    if
node.left:

                        new_q.append( (node.left, node_number) )

                    if
node.right:

                        new_q.append( (node.right, node_number) )

            # Update the path list

            q =
new_q

            

        return
res

          

Problem Link:

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

This problem is easy to solve by using BFS from the tree root.

We use a list to keep track of different paths, where each path is
represented as a number.

【LeetCode OJ】Sum Root to Leaf Numbers,布布扣,bubuko.com

时间: 2024-12-18 23:38:58

【LeetCode OJ】Sum Root to Leaf Numbers的相关文章

<LeetCode OJ> 129. Sum Root to Leaf Numbers

Total Accepted: 74843 Total Submissions: 229553 Difficulty: Medium 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. Fin

LeetCode OJ: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刷题笔记】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题目:Sum Root to Leaf Numbers和Longest Consecutive Sequence

题目一: 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 解题报告

[题目] 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

一. 题目描述 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 || 129、Sum Root to Leaf Numbers

problem: 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,

【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