内容很常规
没有说出三种遍历方式在实际中的应用,比如二叉搜索树下 使用中序遍历可以得到一个有序序列
问题思考
- 给定一组数据,比如 1,3,5,6,9,10。你来算算,可以构建出多少种不同的二叉树?
数组中的排列组合,n!
- 我们讲了三种二叉树的遍历方式,前、中、后序。实际上,还有另外一种遍历方式,也就是按层遍历,你知道如何实现吗?

public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList();if(root==null){return res;}Queue<TreeNode> queue = new LinkedList();queue.add(root);while(!queue.isEmpty()){int tmpLength = queue.size();List<Integer> list = new ArrayList();//内层循环把这层的元素都取出来for(int i = 0;i<tmpLength;i++){TreeNode node = queue.poll();list.add(node.val);if(node.left!=null){queue.add(node.left);}if(node.right!=null){queue.add(node.right);}}res.add(list);}return res;}
