题意:

image.png

解题思路:

  1. 思路:
  2. 递归(DFS):O(n)
  3. 1. 递归一层记录一层数据,每层level+1
  4. BFSO(n)
  5. 1. 先将根节点入队
  6. 2. 创建队列用来保存节点数据;
  7. 4. 对于当前队列中的所有节点,按顺序依次将儿子加入新队列,并将当前节点的值记录在答案中;
  8. 5. 继续下一路队列遍历,直到队列为空

PHP代码实现:

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

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 levelOrder(root *TreeNode) [][]int {
  10. res := make([][]int, 0)
  11. quene := []*TreeNode{}
  12. if root == nil{
  13. return nil
  14. }
  15. quene = append(quene, root)
  16. for len(quene) > 0 {
  17. tmp := []*TreeNode{}
  18. t := make([]int, 0)
  19. for i := 0; i < len(quene); i++{
  20. t = append(t, quene[i].Val)
  21. if quene[i].Left != nil{
  22. tmp= append(tmp, quene[i].Left)
  23. }
  24. if quene[i].Right != nil{
  25. tmp= append(tmp, quene[i].Right)
  26. }
  27. }
  28. quene = tmp
  29. res= append(res, t)
  30. }
  31. return res
  32. }
  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 levelOrder(root *TreeNode) [][]int {
  10. res := make([][]int, 0)
  11. quene := []*TreeNode{}
  12. if root == nil{
  13. return nil
  14. }
  15. quene = append(quene, root)
  16. for len(quene) > 0 {
  17. tmp := []*TreeNode{}
  18. t := make([]int, 0)
  19. for i := 0; i < len(quene); i++{
  20. t = append(t, quene[i].Val)
  21. if quene[i].Left != nil{
  22. tmp= append(tmp, quene[i].Left)
  23. }
  24. if quene[i].Right != nil{
  25. tmp= append(tmp, quene[i].Right)
  26. }
  27. }
  28. quene = tmp
  29. res= append(res, t)
  30. }
  31. return res
  32. }