最好先看一下LeetCode关于树的串行化格式和实际结构的说明。第一遍写理解得出了些偏差,吃了点亏。

解法一:广度优先遍历

第一遍写的。用了各种标志来记录以保证正确维护遍历深度。

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. /**
  11. * Definition for singly-linked list.
  12. * public class ListNode {
  13. * int val;
  14. * ListNode next;
  15. * ListNode(int x) { val = x; }
  16. * }
  17. */
  18. class Solution {
  19. public ListNode[] listOfDepth(TreeNode tree) {
  20. // BFS队列
  21. Queue<TreeNode> queue = new LinkedList<>();
  22. // 答案
  23. List<ListNode> list = new LinkedList<>();
  24. queue.offer(tree);
  25. // 当前遍历深度
  26. int depth = 0;
  27. // 当前层最后一个结点
  28. TreeNode lastTreeNode = tree;
  29. // 下一层最后一个孩子结点
  30. TreeNode tail = tree;
  31. // 当前层已加入答案的最后一个结点
  32. ListNode lastListNode = null;
  33. while (!queue.isEmpty()) {
  34. TreeNode tmp = queue.poll();
  35. // 该层的第一个结点
  36. if (list.size() == depth) {
  37. lastListNode = new ListNode(tmp.val);
  38. list.add(lastListNode);
  39. } else {
  40. ListNode newNode = new ListNode(tmp.val);
  41. lastListNode.next = newNode;
  42. lastListNode = newNode;
  43. }
  44. if (tmp.left != null) {
  45. queue.offer(tmp.left);
  46. tail = tmp.left;
  47. }
  48. if (tmp.right != null) {
  49. queue.offer(tmp.right);
  50. tail = tmp.right;
  51. }
  52. // 当前层已经全部遍历完,进入下一层
  53. if (tmp == lastTreeNode) {
  54. ++depth;
  55. lastTreeNode = tail;
  56. }
  57. }
  58. ListNode[] ans = new ListNode[depth];
  59. return list.toArray(ans);
  60. }
  61. }

解法二:简化解法一

提交完解法一看了下其他人的解法,发现之前自己写复杂了,只需要先记录该层的结点数,然后通过循环全部遍历完就可以直接进入下一层。

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. /**
  11. * Definition for singly-linked list.
  12. * public class ListNode {
  13. * int val;
  14. * ListNode next;
  15. * ListNode(int x) { val = x; }
  16. * }
  17. */
  18. class Solution {
  19. public ListNode[] listOfDepth(TreeNode tree) {
  20. // BFS队列
  21. Queue<TreeNode> queue = new LinkedList<>();
  22. // 答案
  23. List<ListNode> list = new LinkedList<>();
  24. queue.offer(tree);
  25. // 当前遍历深度
  26. int depth = 0;
  27. // 当前层已加入答案的最后一个结点
  28. ListNode lastListNode = null;
  29. while (!queue.isEmpty()) {
  30. int size = queue.size();
  31. for (int i = 0; i < size; ++i) {
  32. TreeNode tmp = queue.poll();
  33. // 该层的第一个结点
  34. if (i == 0) {
  35. lastListNode = new ListNode(tmp.val);
  36. list.add(lastListNode);
  37. } else {
  38. ListNode newNode = new ListNode(tmp.val);
  39. lastListNode.next = newNode;
  40. lastListNode = newNode;
  41. }
  42. if (tmp.left != null) {
  43. queue.offer(tmp.left);
  44. }
  45. if (tmp.right != null) {
  46. queue.offer(tmp.right);
  47. }
  48. }
  49. ++depth;
  50. }
  51. ListNode[] ans = new ListNode[depth];
  52. return list.toArray(ans);
  53. }
  54. }