package cm.com.algorithm.tree;
/**
* 前序遍历2叉树
* 前序遍历是指,对于树中的任意节点来说,先打印这个节点,然后再打印它的左子树,最后打印它的右子树
*
* @author liushuai13@meicai.cn
* @date 2019-06-11 22:14
*/
public class DLRTree {
private static final Node root = new Node("A");
static {
Node b = new Node("B");
Node c = new Node("C");
root.left = b;
root.right = c;
Node d = new Node("D");
Node e = new Node("E");
b.left = d;
b.right = e;
Node f = new Node("F");
Node g = new Node("G");
c.left = f;
c.right = g;
}
public static void main(String[] args) {
tree(root);
}
private static void tree(Node node){
System.out.print(node.value + ",");
if (node.left != null){
tree(node.left);
}
if (node.right != null){
tree(node.right);
}
}
private static class Node{
Node left;
Node right;
String value;
public Node(String value) {
this.value = value;
}
}
}
前序遍历二叉树
最新推荐文章于 2022-04-06 16:41:40 发布