-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathInTree.java
More file actions
73 lines (61 loc) · 1.42 KB
/
Copy pathPathInTree.java
File metadata and controls
73 lines (61 loc) · 1.42 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.wg.offerAlgorithm;
import java.util.Stack;
/*
* 在二叉树寻找符合条件的路径。
*/
public class PathInTree {
public void findPath(BinaryTreeNode node,int key){
if(node==null)
return;
Stack<Integer>path = new Stack<Integer>();
findPath(node,key,path);
}
/**
*
* @param root
* @param key
* @param path
*/
public void findPath(BinaryTreeNode root,int key,Stack<Integer>path){
if(root==null)
return;
// path.push(root.data);
//如果当前节点没有叶子节点,则打印
if(root.left==null && root.right==null){
if(root.data==key){
for(int i:path)
System.out.print(i+" ");
System.out.println(root.data);
}
}
else{
//现将当前节点亚栈,然后递归进行查找
path.push(root.data);
findPath(root.left,key-root.data,path);
findPath(root.right,key-root.data,path);
path.pop();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTreeNode root1=new BinaryTreeNode();
BinaryTreeNode node1=new BinaryTreeNode();
BinaryTreeNode node2=new BinaryTreeNode();
BinaryTreeNode node3=new BinaryTreeNode();
BinaryTreeNode node4=new BinaryTreeNode();
root1.left=node1;
root1.right=node2;
node1.left=node3;
node1.right=node4;
root1.data=10;
node1.data=5;
node2.data=12;
node3.data=4;
node4.data=7;
PathInTree testFindPath=new PathInTree();
testFindPath.findPath(root1, 21);
}
}