当前位置:网站首页>Enter an integer and a binary tree
Enter an integer and a binary tree
2022-07-25 03:01:00 【Rookies also have dreams】
Enter an integer and a binary tree . From the root node to the leaf node, all the nodes form a path . Print out all paths equal to the input integer . For example, enter an integer 22 And the following binary tree
10
/ \
5 12
/ \
4 7
Then print out two paths :10, 12 and 10, 5, 7.Using recursion + The method of backtracking
Ideas :
(1) If the value of the root node is greater than the given value or the root node is empty , Then clear the path ;
(2) If the value of the root node is equal to the given value , You need to determine whether the current node is a leaf node , If it's a leaf node , Is a path that needs to be found , Store in a stack . If it is not , Empty all the saved nodes .
(3) If the sum of the root nodes is less than the given value , Then visit the left and right subtrees respectively public class test4{ public static class Node{ private int value; private Node leftNode; private Node rightNode; public Node(int value,Node leftNode,Node rightNode){ this.value = value; this.leftNode = leftNode; this.rightNode = rightNode; } } public void findpath(Node node,int num){ if (node.value > num || node == null){ return; } // Use the stack to save the nodes on the path Stack<Integer> s = new Stack<>(); int currentsum = 0; findpath(node,num,s,currentsum); } public void findpath(Node node,int num,Stack<Integer> s,int curretnsum){ s.push(node.value); curretnsum += node.value; boolean isleaf = (node.leftNode == null && node.rightNode == null); // If the node is a leaf node , And the path and =sum if (curretnsum == num && isleaf){ // Print path printstack(s); } // If it's not a leaf node Then traverse his nodes if (node.leftNode != null){ findpath(node,num,s,curretnsum); } if (node.rightNode !=null){ findpath(node,num,s,curretnsum); } s.pop(); curretnsum -= node.value; } public void printstack(Stack<Integer> s){ Stack<Integer> tem = new Stack<>(); while (!s.isEmpty()){ tem.add(s.pop()); }while (!tem.isEmpty()){ int num = tem.pop(); System.out.println(num + ""); s.add(num); } } }
边栏推荐
- Flume's study notes
- Error: tomee required to support ear/ejb deployment
- C: wechat chat software instance (wpf+websocket+webapi+entityframework)
- Operator explanation - C language
- Learning Record V
- Class notes (4) (2) -- 572. Compete
- @Retryable @backoff @recover retry the use of annotations
- Tp5.1 include include files (reference public files)
- "Introduction to interface testing" punch in to learn day05: how can a testing framework support restful interfaces?
- Arduino + si5351 square wave generator
猜你喜欢

YouTube Download and (batch) Download

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port

Preliminary foundation JVM

Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development

Dynamic programming -- Digital DP

2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.

Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘

Learning record XIII

List title of force buckle summary
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code
随机推荐
Time formatting
"Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
Flume's study notes
Use pytest + allure to show the chart results (3)
Js a simple way to store several objects in an array
Method of adding kernel in Jupiter notebook
Selenium framework operation steelth.min.js file hides browser fingerprint features
Analysis of FLV packaging
FLASH read / write problem of stm32cubemx
Learning record Xi
PHP record
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
The file in scanpy1.9.1 cannot be read in scanpy1.7.2. The problem is solved
JS foundation -- hijacking of this keyword
"Introduction to interface testing" punch in to learn day09: Micro service interface: how to use mock to solve chaotic call relationships
Riotboard development board series notes (VII) -- the use of framebuffer
JS written test question -- promise, setTimeout, task queue comprehensive question
Backtracking to solve subset problem
Hashcode details
Learning record 12