题意:

image.png

解题思路:

  1. 思路:
  2. 递归(dfs):O(n)
  3. 1. 递归一层记录一层数据,每层level+1,记录层级;
  4. 2. 每层判断奇偶性,奇数的话按照逆序插入,偶数的话按照先后顺序插入

PHP代码实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * class TreeNode {
  4. * public $val = null;
  5. * public $left = null;
  6. * public $right = null;
  7. * function __construct($value) { $this->val = $value; }
  8. * }
  9. */
  10. class Solution {
  11. /**
  12. * @param TreeNode $root
  13. * @return Integer[][]
  14. */
  15. function zigzagLevelOrder($root) {
  16. $res = [];
  17. if (!$root) return $res;
  18. $this->dfs($root, 0, $res);
  19. return $res;
  20. $queue = [];
  21. array_push($queue, $root);
  22. $level = 0;
  23. while (!empty($queue)) {
  24. $list = [];
  25. foreach ($queue as $r) {
  26. if ($level % 2 == 0) {
  27. array_push($list, $r->val);
  28. } else {
  29. array_unshift($list, $r->val);
  30. }
  31. if ($r->left != null) array_push($queue, $r->left);
  32. if ($r->right != null) array_push($queue, $r->right);
  33. array_shift($queue);
  34. }
  35. array_push($res, $list);
  36. $level++;
  37. }
  38. return $res;
  39. }
  40. function dfs($root, $level, &$res) {
  41. if (!$root) return $res;
  42. if ($level == count($res)) {
  43. $res[$level] = [];
  44. }
  45. if ($level % 2 == 0) {
  46. array_push($res[$level], $root->val);
  47. } else {
  48. array_unshift($res[$level], $root->val);
  49. }
  50. $this->dfs($root->left, $level + 1, $res);
  51. $this->dfs($root->right, $level + 1, $res);
  52. }
  53. }

GO代码实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * type TreeNode struct {
  4. * Val int
  5. * Left *TreeNode
  6. * Right *TreeNode
  7. * }
  8. */
  9. func zigzagLevelOrder(root *TreeNode) [][]int {
  10. return zigzagLevelOrders(root);
  11. if root == nil {
  12. return nil
  13. }
  14. queue := []*TreeNode{root}
  15. var res [][]int
  16. index := 0
  17. for {
  18. lq := len(queue)
  19. if lq == 0 {
  20. break
  21. }
  22. res = append(res, make([]int, lq)) // 申请存储空间
  23. for i := 0; i < lq; i++ {
  24. node := queue[0]
  25. queue = queue[1:]
  26. if index&1 == 0 { // 偶数从左往右
  27. res[index][i] = node.Val
  28. } else { // 奇数从右往左
  29. res[index][lq-i-1] = node.Val
  30. }
  31. if node.Left != nil {
  32. queue = append(queue, node.Left)
  33. }
  34. if node.Right != nil {
  35. queue = append(queue, node.Right)
  36. }
  37. }
  38. index++
  39. }
  40. return res
  41. }
  42. func zigzagLevelOrders(root *TreeNode) [][]int {
  43. var res = [][]int{}
  44. dfs(root, 0, &res)
  45. return res
  46. }
  47. func dfs(root *TreeNode, level int, res *[][]int) {
  48. if root == nil {
  49. return
  50. }
  51. if level == len(*res) {
  52. *res = append(*res, []int{})
  53. }
  54. // 偶数压缩到末尾
  55. if level % 2 == 0 {
  56. (*res)[level] = append((*res)[level], root.Val)
  57. } else {
  58. (*res)[level] = append([]int{root.Val}, (*res)[level]...)
  59. }
  60. dfs(root.Left, level+1, res)
  61. dfs(root.Right, level+1, res)
  62. }