标签:
/**
* @author 黄志伟
*/
public class Search{
public static void main(String[] args){
Node A = new Node();
A.setValue(51);
Node B = new Node();
B.setValue(52);
Node C = new Node();
C.setValue(53);
Node D = new Node();
D.setValue(49);
Node E = new Node();
E.setValue(73);
A.setLeftChild(B);
B.setLeftChild(D);
A.setRightChild(C);
C.setRightChild(E);
out(A);
}
/**
*中根递归遍历二叉树,并输出权值大于50的节点
*@param root 根节点
*/
private static void out(Node root){
if(root.getLeftChild() != null){
out(root.getLeftChild());
}
if(root.getValue() > 50){
System.out.println(root.getValue());
}
if(root.getRightChild() != null){
out(root.getRightChild());
}
}
}
/**
* 自定义节点类型
*/
class Node{
private int value = 0;
private Node leftChild = null;
private Node rightChild = null;
public Node getLeftChild(){
return this.leftChild;
}
public void setLeftChild(Node node){
this.leftChild = node;
}
public Node getRightChild(){
return this.rightChild;
}
public void setRightChild(Node node){
this.rightChild = node;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
}
标签:
原文地址:http://www.cnblogs.com/fcat/p/4643746.html