-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsSumTree.java
More file actions
58 lines (53 loc) · 1.27 KB
/
IsSumTree.java
File metadata and controls
58 lines (53 loc) · 1.27 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
package 剑指offer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/**
* 判断一个数字是否存在于一棵数中的一条路径(此树中只包含正整数)
* @author HOU
*通过一个类似栈的数据结构来存储数据,并通过先序遍历来遍历树
*/
public class IsSumTree {
int current=0;
ArrayList<BinaryTreeNode> s=new ArrayList<BinaryTreeNode>();
public void isSum(BinaryTreeNode b,int sum){
if(b==null){
System.out.println("该数组不存在");
return;
}
current+=b.value;
s.add(b);
// System.out.println(s.size());
if(current==sum){
Iterator<BinaryTreeNode> i=s.iterator();
while(i.hasNext()){
BinaryTreeNode b1=i.next();
System.out.println(b1.value);
}
}
else if(current<sum){
if(b.left!=null)
isSum(b.left, sum);
if(b.right!=null)
isSum(b.right, sum);
}
current-=b.value;
s.remove(b);
}
public static void main(String[] args) {
IsSumTree is=new IsSumTree();
BinaryTreeNode b=new BinaryTreeNode(0);
LinkedList<BinaryTreeNode> s=new LinkedList <>();
s.add(b);
for(int i=1;i<10;i++){
BinaryTreeNode b1=s.remove();
b1.left=new BinaryTreeNode(i);
b1.right=new BinaryTreeNode(++i);
if(b1.left!=null)
s.add(b1.left);
if(b1.right!=null)
s.add(b1.right);
}
is.isSum(b, 15);
}
}