每日一题 为了工作 2020 0407 第三十六题
/**
*
* 问题:利用递归方式实现二叉树先序遍历
* 先序遍历 根 左 右
* @author 雪瞳
*
*/
public class Node {
public int value;
public Node left;
public Node right;
public Node(int data){
this.value=data;
}
}
public class PreOrderRecur {
public void preOrderRecur(Node head){
Node current = head;
if(current == null){
return;
}
System.out.print(head.value+"\t");
preOrderRecur(current.left);
preOrderRecur(current.right);
}
}
public class TestOrder {
public static void main(String[] args) {
Node root = new Node(1);
Node left = new Node(2);
Node right = new Node(3);
Node left2 = new Node(4);
Node left3 = new Node(5);
Node left4 = new Node(6);
Node right2 = new Node(7);
/**
*
* 1
* 2 3
* 4
* 5
* 6 7
*/
root.left=left;
root.right=right;
left.left=left2;
left2.left=left3;
left3.left=left4;
left3.right=right2;
PreOrderRecur por = new PreOrderRecur();
por.preOrderRecur(root);
}
}
* 运行结果