zoukankan      html  css  js  c++  java
  • [leetcode] Binary Tree Level Order Traversal

    Binary Tree Level Order Traversal Ⅰ

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

    For example:
    Given binary tree [3,9,20,null,null,15,7],

        3
       / 
      9  20
        /  
       15   7
    

    return its level order traversal as:

    [
      [3],
      [9,20],
      [15,7]
    ]

    分析:题目翻译一下:对一个二叉树进行层次遍历,BFS。我比较喜欢用队列来做,BFS比较基础了,也很简单。
    代码如下:
     1 class Solution {
     2     public List<List<Integer>> levelOrder(TreeNode root) {
     3         List<List<Integer>> res = new ArrayList<>();
     4         if ( root == null ) return res;
     5         Queue<TreeNode> queue = new LinkedList<>();
     6         queue.offer(root);
     7         while ( !queue.isEmpty() ){
     8             int size = queue.size();
     9             List<Integer> list = new ArrayList<>();
    10             for ( int i = 0 ; i < size ; i ++ ){
    11                 TreeNode t = queue.poll();
    12                 list.add(t.val);
    13                 if ( t.left != null )  queue.offer(t.left);
    14                 if ( t.right != null ) queue.offer(t.right);
    15             }
    16             res.add(list);
    17         }
    18         return res;
    19     }
    20 }

        运行时间3ms。

    Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

    For example:
    Given binary tree [3,9,20,null,null,15,7],

        3
       / 
      9  20
        /  
       15   7
    

    return its bottom-up level order traversal as:

    [
      [15,7],
      [9,20],
      [3]
    ]

    分析:题目翻译一下:倒着层次遍历一颗二叉树
    思路1:根据上面的案例,也就是将BFS结果倒着输出就可以了。
    这里考察一个只是点就是如何将list倒序,使用Collections.reverse(list)这个方法。注:Collections这个类里面有很多对于list、map这些集合对象的方法。有空可以查一下。
     1 public List<List<Integer>> levelOrder(TreeNode root) {
     2         List<List<Integer>> res = new ArrayList<>();
     3         Queue<TreeNode> queue = new LinkedList<>();
     4         queue.offer(root);
     5         while ( !queue.isEmpty() ){
     6             int size = queue.size();
     7             List<Integer> list = new ArrayList<>();
     8             for ( int i = 0 ; i < size ; i ++ ){
     9                 TreeNode t = queue.poll();
    10                 list.add(t.val);
    11                 if ( t.left != null )  queue.offer(t.left);
    12                 if ( t.right != null ) queue.offer(t.right);
    13             }
    14             res.add(list);
    15         }
    16         Collections.reverse(res);
    17         return res;
    18     }

    思路二:将层的结果放入到一个stack中,最后读取stack中的数据也可以实现倒序。就不写代码了。

     
  • 相关阅读:
    Spring-Boot:多种配置注入方式
    Spring-Boot:Profile简单示例
    Spring-Boot:拦截器注解范例
    Docker:镜像的迁移
    YARN的内存和CPU配置
    Spark On YARN内存分配
    Spark配置参数
    linux_密钥
    分布式架构高可用架构篇_04_Keepalived+Nginx实现高可用Web负载均衡
    PythonDay01
  • 原文地址:https://www.cnblogs.com/boris1221/p/9780954.html
Copyright © 2011-2022 走看看