import java.util.Queue; import java.util.LinkedList; class TreeNode{ public int val; public TreeNode left; public TreeNode right; public TreeNode(int val) { this.val = val; this.left = null; this.right = null; } }
public class Main { public static void main(String[] args) { TreeNode root = new TreeNode(1); TreeNode node2 = new TreeNode(2); TreeNode node3 = new TreeNode(3); TreeNode node4 = new TreeNode(4); TreeNode node5 = new TreeNode(5); TreeNode node6 = new TreeNode(6); TreeNode node7 = new TreeNode(7); root.left = node2; root.right = node3; node2.left = node4; node2.right = node5; node3.left = node6; node3.right = node7;
Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root);
整个看下来还是感觉迷迷糊糊的
按照层次遍历的顺序存储
按照从上到下,从左到右的顺序树存,一般二叉树会增添一些空节点形成完全二叉树