【LeetCode with Python】 Sum Root to Leaf Numbers
2014-07-06 15:32
423 查看
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/
题目类型:递归回溯,二叉树
难度评价:★
本文地址:/article/1377498.html
Given a binary tree containing digits from
An example is the root-to-leaf path
Find the total sum of all root-to-leaf numbers.
For example,
The root-to-leaf path
The root-to-leaf path
Return the sum = 12 + 13 =
从根节点到叶子节点的路径代表一个数值,找出所有的这些数值然后累加得到一个和。
搜索算法可以用来找可行解,或者找出全部解。找可行解的时候可以用深度优先遍历DFS,或者广度优先遍历BFS。而这个题目则是找出全部解,然后累加全部解。而搜索算法找全部解时的递归解法一般也是两种:自底向上和自顶向下(自己命名的,哈哈)。
就拿这道题来说,自底向上,也就是当递归到叶子节点后,在递归退栈的过程中,让左子树递归返回的数加上右子树递归返回的数。
而自顶向下,其实就是所谓的尾递归了。判断一个递归是不是尾递归,当递归调用是整个函数体中最后执行的语句且它的返回值不属于表达式的一部分时,这个递归调用就是尾递归。
原题页面:https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/
题目类型:递归回溯,二叉树
难度评价:★
本文地址:/article/1377498.html
Given a binary tree containing digits from
0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path
1->2->3which 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->2represents the number
12.
The root-to-leaf path
1->3represents the number
13.
Return the sum = 12 + 13 =
25.
从根节点到叶子节点的路径代表一个数值,找出所有的这些数值然后累加得到一个和。
搜索算法可以用来找可行解,或者找出全部解。找可行解的时候可以用深度优先遍历DFS,或者广度优先遍历BFS。而这个题目则是找出全部解,然后累加全部解。而搜索算法找全部解时的递归解法一般也是两种:自底向上和自顶向下(自己命名的,哈哈)。
就拿这道题来说,自底向上,也就是当递归到叶子节点后,在递归退栈的过程中,让左子树递归返回的数加上右子树递归返回的数。
class Solution: def doSumNumbers(self, root, val): if None == root.left and None == root.right: return val * 10 + root.val left = 0 right = 0 if None != root.left: left = self.doSumNumbers(root.left, val * 10 + root.val) if None != root.right: right = self.doSumNumbers(root.right, val * 10 + root.val) return left + right # @param root, a tree node # @return an integer def sumNumbers(self, root): if None == root: return 0 return self.doSumNumbers(root, 0)
而自顶向下,其实就是所谓的尾递归了。判断一个递归是不是尾递归,当递归调用是整个函数体中最后执行的语句且它的返回值不属于表达式的一部分时,这个递归调用就是尾递归。
class Solution: def __init__(self): self.sum = 0 def doSumNumbers(self, root, val): if None == root.left and None == root.right: self.sum += val * 10 + root.val if None != root.left: self.doSumNumbers(root.left, val * 10 + root.val) if None != root.right: self.doSumNumbers(root.right, val * 10 + root.val) # @param root, a tree node # @return an integer def sumNumbers(self, root): if None == root: return 0 self.doSumNumbers(root, 0) return self.sum
相关文章推荐
- [Leetcode][python]Sum Root to Leaf Numbers
- LeetCode--Sum Root to Leaf Numbers(Python)
- [LeetCode]题解(python):129-Sum Root to Leaf Numbers
- 【LEETCODE】129-Sum Root to Leaf Numbers [Python]
- [leetcode]Sum Root to Leaf Numbers @ Python
- [leetcode]Sum Root to Leaf Numbers
- [LeetCode 129] - 根节点到叶子节点数字求和(Sum Root to Leaf Numbers)
- [leetcode] Sum Root to Leaf Numbers
- LeetCode - Sum Root to Leaf Numbers
- [LeetCode]Sum Root to Leaf Numbers
- leetcode: Sum Root to Leaf Numbers
- [Leetcode 39] 129 Sum Root to Leaf Numbers
- LeetCode: Sum Root to Leaf Numbers
- [LeetCode] Sum Root to Leaf Numbers, Solution
- leetcode Sum Root to Leaf Numbers(所有路径之和)
- leetcode Sum Root to Leaf Numbers 二叉树所有叶节点的路径和
- LeetCode-Sum Root to Leaf Numbers
- LeetCode : Sum Root to Leaf Numbers
- leetcode Sum Root to Leaf Numbers
- [LeetCode] Sum root to leaf numbers