常见的二叉树:
二叉树定义:
在计算机科学中,二叉树是每个结点最多有两个子树的树结构
demo:
1、创建节点类:Node
public class Node { private Node left; // 左子节点 private Node right; // 右子节点 private int data; // 节点的值 public Node(int data){ this.left = null; this.right = null; this.data = data; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public int getData() { return data; } public void setData(int data) { this.data = data; } } // 以中序遍历的方式输出: @Override public String toString() { return (left == null?"":(left.toString() + ",")) + data + (right == null?"":("," + right.toString() )); } }
参考文章:
https://blog.csdn.net/yhhazr/article/details/7944089
https://blog.csdn.net/zlp1992/article/details/51406067