Skip to content

Instantly share code, notes, and snippets.

@azeemmohd
Created January 16, 2015 15:46
Show Gist options
  • Save azeemmohd/60c1b5ebbdc1a634c582 to your computer and use it in GitHub Desktop.
Save azeemmohd/60c1b5ebbdc1a634c582 to your computer and use it in GitHub Desktop.
public static void sumOfNumsFormedByPaths(Node root, ArrayList<String> nums, String sb) {
if(root == null) {
return;
}
sb+=root.data;
if(root.right == null && root.left == null) {
nums.add(sb);
}
sumOfNumsFormedByPaths(root.left, nums, sb);
sumOfNumsFormedByPaths(root.right, nums, sb);
}
public static void main(String [] args) {
Node root = new Node(6);
Node three = new Node(3);
Node five = new Node(5);
root.left = three;
root.right = five;
Node five2 = new Node(5);
five2.left = new Node(7);
five2.right = new Node(4);
five.right = new Node(4);
Node two = new Node(2);
three.left = two;
three.right = five2;
ArrayList<String> nums = new ArrayList<String>();
sumOfNumsFormedByPaths(root, nums, "");
int sum = 0;
for(String s : nums) {
sum += Integer.parseInt(s);
}
System.out.println(sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment