Created
January 16, 2015 15:46
-
-
Save azeemmohd/60c1b5ebbdc1a634c582 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
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