#Leet Code# Root to leaf

语言:Python

描述:使用递归实现

 1     def getList(self, node):
 2         if node is None:
 3             return []
 4
 5         if node.left is None and node.right is None:
 6             return [[node.val]]
 7
 8         result = []
 9         for item in self.getList(node.left):
10             result.append([node.val] + item)
11
12         for item in self.getList(node.right):
13             result.append([node.val] + item)
14
15         return result
16
17     def getNumByList(self, lst):
18         result = 0
19         for item in lst:
20             result *= 10
21             result += item
22         return result
23
24     def sumNumbers(self, root):
25         result = self.getList(root)
26         sum = 0
27         for item in result:
28             sum += self.getNumByList(item)
29         return sum

新的实现

 1 class Solution:
 2     # @param root, a tree node
 3     # @return an integer
 4     def sumPath(self, node, value):
 5         if node is None:
 6             return 0
 7
 8         value = value * 10 + node.val
 9         if (node.left is None and node.right is None):
10             self.sum += value
11
12         self.sumPath(node.left, value)
13         self.sumPath(node.right, value)
14
15     def sumNumbers(self, root):
16         self.sum = 0
17         self.sumPath(root, 0)
18
19         return self.sum   

#Leet Code# Root to leaf

时间: 2024-11-05 00:50:13

#Leet Code# Root to leaf的相关文章

#Leet Code# Populating Next Right Pointers in Each Node II

描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立. 代码: 1 class Solution: 2 # @param root, a tree node 3 # @return nothing 4 def doSth(self, nextNode, conNode): 5 while nextNode is not

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

Sum Root to Leaf Numbers

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 number

[leetcode]_Sum Root to Leaf Numbers

题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: 1 private int sum = 0; 2 public int sumNumbers(TreeNode root) { 3 dfs(root , 0); 4 return sum; 5 } 6 public void dfs(TreeNode node , int tempSum){ 7 if(node == null) retur

leetcode --day12 Surrounded Regions & Sum Root to Leaf Numbers & Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

LeetCode_Sum Root to Leaf Numbers

一.题目 Sum Root to Leaf Numbers Total Accepted: 47437 Total Submissions: 156443My 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 repre

LeetCode: Sum Root to Leaf Numbers 解题报告

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 number

23. Sum Root to Leaf Numbers

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 number