/**
* 从左到右生成叶子节点单链表。头指针是最左边节点。
*
*
*/
public class LeafList {
public Node head = null;
public Node current = null;
public void handle(Node node){
if(node.left==null && node.right==null){
if(head == null){
head = node;
current = node;
}else{
current.right = node;
current = node;
}
}
if(node.left != null){
handle(node.left);
}
if(node.right != null){
handle(node.right);
}
}
public void printList(Node head){
Node current = head;
while(current != null){
System.out.print(current.value + " ");
current = current.right;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree bt = new BinaryTree();
int[] a = {7,2,4,8,3,5,1,9,0,6};
Node root = bt.buildTree(a);
bt.printTree();
LeafList ll = new LeafList();
ll.handle(root);
ll.printList(ll.head);
}
}
叶子节点单链表
最新推荐文章于 2024-09-18 08:50:37 发布