博客
关于我
力扣 - 102. 二叉树的层序遍历
阅读量:450 次
发布时间:2019-03-06

本文共 1754 字,大约阅读时间需要 5 分钟。

目录

题目

思路一(迭代)

采用广度优先搜索(BFS)的方法,利用队列的先进先出特性遍历树节点。

  • BFS广度优先搜索
  • 使用队列来实现先进先出的特性

代码

import java.util.*;  public class Solution {      public List levelOrder(TreeNode root) {          List res = new LinkedList<>();          if (root == null) {              return res;          }          Deque queue = new LinkedList<>();          queue.offer(root);          while (!queue.isEmpty()) {              List level = new LinkedList<>();              int size = queue.size();              while (size > 0) {                  TreeNode node = queue.poll();                  level.add(node.val);                  if (node.left != null) {                      queue.offer(node.left);                  }                  if (node.right != null) {                      queue.offer(node.right);                  }                  size--;              }              res.add(level);          }          return res;      }  }

复杂度分析

该方法的时间复杂度为O(N),因为每个节点只会被访问一次。空间复杂度同样为O(N),因为在最坏情况下队列会保存所有节点。

思路二(递归)

采用深度优先搜索(DFS)的方法,递归地遍历树节点,并将每一层的节点值按层存储。

  • DFS深度优先搜索
  • 递归函数中使用索引来表示当前处理的层数
  • 每次递归调用时,将当前节点值添加到对应的层中

代码

import java.util.*;  public class Solution {      public List levelOrder(TreeNode root) {          List res = new LinkedList<>();          if (root == null) {              return res;          }          dfs(1, res, root);          return res;      }      private void dfs(int index, List res, TreeNode root) {          if (root == null) {              return;          }          if (res.size() < index) {              res.add(new LinkedList<>());          }          res.get(index - 1).add(root.val);          dfs(index + 1, res, root.left);          dfs(index + 1, res, root.right);      }  }

复杂度分析

该方法的时间复杂度也是O(N),因为每个节点都会被访问一次。空间复杂度为O(h),其中h为树的高度,这是因为递归过程中每一层都需要存储当前层的节点值。

转载地址:http://lspyz.baihongyu.com/

你可能感兴趣的文章
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>