package test;
public class Test {
public int[] values = { 1, 2, 4, 0, 0, 5, 0, 0, 3, 6, 0, 0, 7, 0, 0 };
public int i = 0;
public static void main(String[] args) {
Test test = new Test();
TreeNode root = test.createBinaryTree();
test.travelBinaryTree(root);
}
public TreeNode createBinaryTree() {
TreeNode root = null;
int val = values[i++];
if (val != 0) {
root=new TreeNode();
root.setVal(val);
root.setLeftNode(createBinaryTree());
root.setRightNode(createBinaryTree());
}
return root;
}
public void travelBinaryTree(TreeNode root) {
if (root != null) {
travelBinaryTree(root.getLeftNode());
travelBinaryTree(root.getRightNode());
System.out.print(root.getVal()+"\t");
}
}
public class TreeNode {
private Integer val;
private TreeNode leftNode;
private TreeNode rightNode;
public Integer getVal() {
return val;
}
public void setVal(Integer val) {
this.val = val;
}
public TreeNode getLeftNode() {
return leftNode;
}
public void setLeftNode(TreeNode leftNode) {
this.leftNode = leftNode;
}
public TreeNode getRightNode() {
return rightNode;
}
public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
}
}