zoukankan      html  css  js  c++  java
  • 二叉树的层次遍历 II

    给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

    例如:
    给定二叉树 [3,9,20,null,null,15,7],

    3
    / 
    9 20
    / 
    15 7


    返回其自底向上的层次遍历为:

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

    题解1: DFS实现层次遍历
    使用深搜来实现层次遍历的核心就是必须记录住当前是第几层,只有知道了当前是第几层才能往对应层的集合添加数据

     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 class Solution {
    11     public List<List<Integer>> levelOrderBottom(TreeNode root) {
    12         List<List<Integer>> list = new ArrayList<>();
    13         if (root == null) {
    14             return list;
    15         }
    16         //深搜
    17         dfs(root, 0, list);
    18         
    19         //反转
    20         Collections.reverse(list);
    21         return list;
    22     }
    23 
    24     private void dfs(TreeNode root, int lelve, List<List<Integer>> list) {
    25         if (root == null) {
    26             return ;
    27         }
    28         
    29         if (list.size() <= lelve) {
    30             //说明当前层,还没有开始存数据,进行初始化
    31             list.add(lelve, new ArrayList<Integer>());
    32         }
    33         //将当前节点的数据存储到当前层
    34         list.get(lelve).add(root.val);
    35         
    36         //继续遍历遍历下一层的数据
    37         dfs(root.left, lelve + 1, list);
    38         dfs(root.right, lelve + 1, list);
    39     }
    40 }

    题解2:队列实现层序遍历

     1  // 迭代
     2 class Solution {
     3     public List<List<Integer>> levelOrderBottom(TreeNode root) {
     4         List<List<Integer>> ans = new ArrayList<>();
     5         Queue<TreeNode> queue = new LinkedList<>();
     6 
     7         if (root == null) return ans;
     8 
     9         // 记录层
    10         int levels = 0;
    11         queue.add(root);
    12         while (!queue.isEmpty())
    13         {
    14             ans.add(new ArrayList<Integer>());
    15             int len = queue.size();
    16             for (int i = 0; i < len; i++)
    17             {
    18                 root = queue.remove();
    19                 ans.get(levels).add(root.val);
    20                 if (root.left != null) queue.add(root.left);
    21                 if (root.right != null) queue.add(root.right);
    22             }
    23             levels ++;
    24         }
    25         // 跟传统层序遍历比,多了个反转。
    26         Collections.reverse(ans);
    27         return ans;
    28     }
    29 }

    题解3:队列妙用

     1 class Solution {
     2     public List<List<Integer>> levelOrderBottom(TreeNode root) {
     3         LinkedList<List<Integer>> result = new LinkedList<>();
     4         if (root == null)
     5             return result;
     6         Queue<TreeNode> queue = new LinkedList<>();
     7         queue.add(root);
     8         while (!queue.isEmpty()) {
     9             List<Integer> oneLevel = new ArrayList<>();
    10             // 每次都取出一层的所有数据
    11             int count = queue.size();
    12             for (int i = 0; i < count; i++) {
    13                 TreeNode node = queue.poll();
    14                 oneLevel.add(node.val);
    15                 if (node.left != null)
    16                     queue.add(node.left);
    17                 if (node.right != null)
    18                     queue.add(node.right);
    19             }
    20             // 每次都往队头塞
    21             result.addFirst(oneLevel);
    22         }
    23         return result;
    24     }
    25 }


    链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii

  • 相关阅读:
    新浪微盘又是一个给力的产品啊,
    InfoQ: 百度数据库架构演变与设计
    列式数据库——Sybase IQ
    MapR初体验 淘宝共享数据平台 tbdata.org
    IBM正式发布新一代zEnterprise大型机(组图) 大型机,IBM,BladeCenter,美国,纽约 TechWeb News
    1TB is equal to the number of how many GB? 1PB equal to is equal to the number of TB? 1EB PB? | PCfault.com
    Cassandra vs HBase | WhyNosql
    The Hadoop Community Effect
    雅虎剥离开源软件平台 Hadoop ,与风投新建 Hortonworks 公司 品味雅虎
    RowOriented Database 、ColumnOriented Database 、KeyValue Store Database 、DocumentOriented Database
  • 原文地址:https://www.cnblogs.com/treasury/p/12611184.html
Copyright © 2011-2022 走看看