递归遍历二叉树并且求和
一、 创建结点
public class TreeNode {
private int value;
private TreeNode lnode;
private TreeNode rnode;
构造方法+get、set方法
public TreeNode() {
super();
// TODO Auto-generated constructor stub
}
public TreeNode(int value, TreeNode lnode, TreeNode rnode) {
super();
this.value = value;
this.lnode = lnode;
this.rnode = rnode;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public TreeNode getLnode() {
return lnode;
}
public void setLnode(TreeNode lnode) {
this.lnode = lnode;
}
public TreeNode getRnode() {
return rnode;
}
public void setRnode(TreeNode rnode) {
this.rnode = rnode;
}
}
二、 递归二叉树的方法
package com.zzy.tree;
public class BinaryTree {
static int sum=0;
//创建sum变量
/**
* 先序遍历
*
* @param root
*/
public int preorder(Tre