Created
July 19, 2014 01:33
-
-
Save hilda8519/b1c2e0971ddffbaf0597 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class CheckSum { | |
private class Node{ | |
public int val; | |
public Node left; | |
public Node right; | |
Node(int n){ | |
val=n; | |
left=null; | |
right=null; | |
} | |
} | |
class ListNode{ | |
public Object data; | |
int val; | |
ListNode next; | |
ListNode(int i){ | |
val=i; | |
next=null; | |
} | |
public void CheckSum(Node node, int sum,int[] path, int level){ | |
if(node==null){ | |
return; | |
} | |
path[level]=node.data; | |
int n=0; | |
for(int i=level;i>=0;i--){ | |
n=path[i]++; | |
if(n==sum){ | |
println(path,i,level); | |
} | |
} | |
Find(node.left,sum,path,level+1); | |
Find(node.right,sum,path,level+1); | |
path[level]=Integer.MIN_VALUE; | |
} | |
public void Find(Node node, int sum,int path, int level){ | |
int depth=depth(node); | |
int[] path=new int[depth]; | |
Find(node, sum,path,0); | |
} | |
public int depth(Node node){ | |
if(node==null){ | |
return 0; | |
} | |
else{ | |
return 1+Math.max(depth(node.left),depth(node.right)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment