# LeetCode算法题-Binary Tree Level Order Traversal II（Java实现）

### 01 看题和准备

    3
/ \
9  20
/ \
15  7

### 02 第一种解法

public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list2 = new ArrayList<>();
if (root == null) {
return list2;
}
List<Integer> list = new ArrayList<Integer>();
while (!q.isEmpty()) {
list = new ArrayList<Integer>();
while (!q.isEmpty()) {
TreeNode t = q.poll();
if (t.left != null) {
}
if (t.right != null) {
}
}
q = tem;
}
List<List<Integer>> list3 = new ArrayList<>();
for(int i=list2.size()-1; i >= 0; i--){
}
return list3;
}

### 03 第二种解法

public List<List<Integer>> levelOrderBottom2(TreeNode root) {
List<List<Integer>> list2 = new ArrayList<>();
if (root == null) {
return list2;
}
List<Integer> list = new ArrayList<Integer>();
q.offer(root);
while (!q.isEmpty()) {
list = new ArrayList<Integer>();
int n = q.size();
while (n-- > 0) {
TreeNode t = q.poll();
if (t.left != null) {
q.offer(t.left);
}
if (t.right != null) {
q.offer(t.right);
}
}
}
List<List<Integer>> list3 = new ArrayList<>();
for(int i=list2.size()-1; i >= 0; i--){
}
return list3;
}

### 04 第三种解法

public List<List<Integer>> levelOrderBottom3(TreeNode root) {
List<List<Integer>> list2 = new ArrayList<>();
if (root == null) {
return list2;
}
List<Integer> list = new ArrayList<Integer>();
Stack<List<Integer>> stack = new Stack<List<Integer>>();
q.offer(root);
while (!q.isEmpty()) {
list = new ArrayList<Integer>();
int n = q.size();
while (n-- > 0) {
TreeNode t = q.poll();
if (t.left != null) {
q.offer(t.left);
}
if (t.right != null) {
q.offer(t.right);
}
}
}
while (!stack.isEmpty()) {
}
return list2;
}

### 05 第四种解法

public List<List<Integer>> levelOrderBottom4(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<List<Integer>> node = new ArrayList<List<Integer>>();
viewTree(root, res, 0);
for (int i=res.size()-1; i>=0; i--) {
}
return node;
}

public void viewTree(TreeNode root, List<List<Integer>> res, int deep) {
if(root == null)
return;
if (res.size() <= deep) {
List<Integer> node = new ArrayList<Integer>();
} else {
List<Integer> node = (List<Integer>)res.get(deep);
}
viewTree(root.left, res, deep+1);
viewTree(root.right, res, deep+1);
}

### 06 验证与测试

public static void main(String[] args) {
Easy_107_BinaryTreeLevelOrderTraversalII instance = new Easy_107_BinaryTreeLevelOrderTraversalII();
TreeNode t = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(5);
TreeNode t6 = new TreeNode(6);
TreeNode t7 = new TreeNode(7);
TreeNode t8 = new TreeNode(8);
t.left = t2;
t.right = t3;
t2.left = t4;
t2.right = t5;
t3.left = t6;
t3.right = t7;
t7.left = t8;
long start = System.nanoTime();
List<List<Integer>> result = instance.levelOrderBottom(t);
long end = System.nanoTime();
System.out.println("levelOrderBottom---输出："+result.toString()+" , 用时："+(end-start)/1000+"微秒");
long start2 = System.nanoTime();
List<List<Integer>> result2 = instance.levelOrderBottom2(t);
long end2 = System.nanoTime();
System.out.println("levelOrderBottom2---输出："+result2.toString()+" , 用时："+(end2-start2)/1000+"微秒");
long start3 = System.nanoTime();
List<List<Integer>> result3 = instance.levelOrderBottom3(t);
long end3 = System.nanoTime();
System.out.println("levelOrderBottom3---输出："+result3.toString()+" , 用时："+(end3-start3)/1000+"微秒");
long start4 = System.nanoTime();
List<List<Integer>> result4 = instance.levelOrderBottom4(t);
long end4 = System.nanoTime();
System.out.println("levelOrderBottom4---输出："+result4.toString()+" , 用时："+(end4-start4)/1000+"微秒");
}

levelOrderBottom---输出：[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时：446微秒
levelOrderBottom2---输出：[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时：24微秒
levelOrderBottom3---输出：[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时：55微秒
levelOrderBottom4---输出：[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时：23微秒

### 07 小结

posted @ 2018-11-08 08:18 小川94 阅读(...) 评论(...) 编辑 收藏