I wanted to know the sum of all odd nodes and was programming it in a resursive way. But i cant make it work so maybe you guys can help me...
This is my BinNode Class
package com.company;
public class BinNode {
public int element;
public BinNode left, right;
public BinNode(int e, BinNode l, BinNode r) {
element = e;
left = l;
right = r;
}
public BinNode(int e) {
element = e;
left = null;
right = null;
}
}
and now my method which first looks if the tree is empty otherwise if its not its gonna call the recursive method
public int countOdd() {
if(root == null) {
return 0;
}
return countOdd_rek(root);
}
and now the method
private int countOdd_rek(BinNode bn) {
int odd, oddRight, oddLeft;
odd = oddRight = oddLeft = 0;
if(bn.left != null) {
if(bn.left.element % 2 != 0) { //add +1 if bn.left is odd
oddLeft +=1;
}
countOdd_rek(bn.left);
}
if(bn.right != null) {
if(bn.right.element % 2 != 0) { //add +1 if bn.right is odd
oddRight +=1;
}
countOdd_rek(bn.right);
}
//add +1 if root is also odd
if(bn.element % 2 != 0)
odd = 1 + oddLeft + oddRight;
//add nothing if root isnt odd
if(bn.element % 2 == 0)
odd = 0 + oddLeft + oddRight;
return odd;
}
maybe someone could help it would be very nice... thanks
EDIT I SOLVED IT SOLUTION:
private int countOdd_rek(BinNode bn) {
int odd, oddRight, oddLeft;
odd = oddRight = oddLeft = 0;
if(bn.left != null) {
oddLeft += countOdd_rek(bn.left);
}
if(bn.right != null) {
oddRight += countOdd_rek(bn.right);
}
//add +1 if root is also odd
if(bn.element % 2 != 0)
odd = 1 + oddLeft + oddRight;
//add nothing if root isnt odd
if(bn.element % 2 == 0)
odd = 0 + oddLeft + oddRight;
return odd;
}
Your main problem is that you're not actually doing anything with the values of countOdd_rek(...)
. Also, you're defining local variables to store the odd counts which will get overridden with each recursive method call. A cleaner way to do it would be this:
public class BinNode {
public int element;
public BinNode left, right;
public BinNode(int e, BinNode l, BinNode r) {
element = e;
left = l;
right = r;
}
public BinNode(int e) {
element = e;
left = null;
right = null;
}
public static int countOdd(BinNode root) {
return countOdd_rek(root);
}
private static int countOdd_rek(BinNode bn) {
if (bn == null) {
return 0;
}
if (bn.element % 2 != 0) {
//add +1 if root is also odd
return 1 + countOdd_rek(bn.left) + countOdd_rek(bn.right);
} else {
return countOdd_rek(bn.left) + countOdd_rek(bn.right);
}
}
public static void main(String[] args) {
BinNode root = new BinNode(1,
new BinNode(2,
new BinNode(4, null, null),
new BinNode(5, null, null)),
new BinNode(3,
new BinNode(6, null, null),
new BinNode(7, null, null)));
System.out.println(countOdd(root));
}
}
I added an example for you in the main method, too.
Let me know if you have any questions.
EDIT: Just saw that you solved it yourself. Nice job--I guess you could look at my code as a reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.