Created
March 15, 2013 20:47
-
-
Save CaglarGonul/5172998 to your computer and use it in GitHub Desktop.
A binary tree consists of int.
This file contains hidden or 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 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