当前位置:网站首页>Leetcode-129- sum of numbers from root node to leaf node
Leetcode-129- sum of numbers from root node to leaf node
2022-06-11 21:27:00 【z754916067】
subject
- Find the sum of numbers from root node to leaf node
Give you the root node of a binary tree root , Each node in the tree stores a 0 To 9 Number between .
Each path from the root node to the leaf node represents a number :
for example , Path from root node to leaf node 1 -> 2 -> 3 Representation number 123 .
Calculates the generated from the root node to the leaf node The sum of all the figures .
Leaf nodes A node without children .
Ideas
- One glance recursion , How do binary trees always recurse , It's a bit of a backtrack .
- It's quite simple , If binary tree recursion is too much, it will become familiar .
Code
int sum=0;
public int sumNumbers(TreeNode root) {
if(root.left==null && root.right==null) return root.val;
// recursive Record the current number
// The root node
int val = root.val;
LinkedList<Integer> ll = new LinkedList<>();
ll.add(val);
if(root.left!=null) DFS(root.left,ll);
if(root.right!=null) DFS(root.right,ll);
return sum;
}
public void DFS(TreeNode root,LinkedList<Integer> ll){
// Go to the leaf node to indicate that the calculation is ready
if(root.left==null && root.right==null){
ll.add(root.val);
int temp=0;
int nums=1;
for(int i=ll.size()-1;i>=0;i--){
temp+=ll.get(i)*nums;
nums*=10;
}
sum+=temp;
ll.removeLast();
return;
}
// otherwise Add it to ll
ll.add(root.val);
if(root.left!=null) DFS(root.left,ll);
if(root.right!=null) DFS(root.right,ll);
ll.removeLast();
return;
}
边栏推荐
- Codeforces Round #744 (Div. 3) 解题报告
- JVM|类加载器;双亲委派机制
- Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system
- How to Load Data from CSV (Data Preparation Part)
- BZOJ3189 : [Coci2011] Slika
- JS performs non empty judgment on various data types of the returned data.
- How to import workflows provided on SAP API hub to sap BTP
- LeetCode-76-最小覆盖子串
- Database daily question --- day 9: salesperson
- 12 golden rules of growth
猜你喜欢

使用 float 创建一个网页页眉、页脚、左边的内容和主要内容。

Syntax of SQL

Solve the problem of img 5px spacing

Cs144 lab0 lab1 record

How to Load Data from CSV (Data Preparation Part)

Use float to create a page header, footer, left content, and main content.

JS performs non empty judgment on various data types of the returned data.

Goto statement of go language

Software test plan

flutter系列之:flutter中常用的container layout详解
随机推荐
flutter系列之:flutter中常用的container layout详解
Game client performance (memory) [previous]
【C語言進階】整型在內存中的存儲
2022年6月9日 16:29:41 日记
Website online customer service system Gofly source code development log - 2 Develop command line applications
BZOJ3189 : [Coci2011] Slika
字符串复制函数
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
RANSAC extraction plane (matlab built-in function)
Syntax of SQL
Go语言条件语句
[advanced C language] integer storage in memory
A collection of commonly used open source data sets for face recognition
Software test plan
常用文件函数
如何将SAP API Hub 上提供的工作流导入到 SAP BTP 上
Refresh and upgrade | innovation, starting from cloud store
Solve the problem of img 5px spacing
Educational codeforces round 111 (rated for Div. 2) C Supplement
BZOJ3189 : [Coci2011] Slika