Skip to content

Instantly share code, notes, and snippets.

@CaglarGonul
Created March 15, 2013 20:47
Show Gist options
  • Save CaglarGonul/5172998 to your computer and use it in GitHub Desktop.
Save CaglarGonul/5172998 to your computer and use it in GitHub Desktop.
A binary tree consists of int.
public class BinaryTree {
int i;
BinaryTree right;
BinaryTree left;
public BinaryTree(int _i, BinaryTree _right, BinaryTree _left) {
i =_i;
right = _right;
left = _left;
}
public int sumAll(){
int ans = this.i;
if(this.left != null){
ans += this.left.sumAll();
}
if(this.right != null){
ans += this.right.sumAll();
}
return ans;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(this.i);
if(this.left != null){
sb.append(left.toString());
}
if(this.right != null){
sb.append(right.toString());
}
sb.append(")");
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment