• 107. 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,#,#,15,7},

        3
       / 
      9  20
        /  
       15   7
    

    return its bottom-up level order traversal as:

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

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

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

    一刷,比102题只多了最后一步,还是用了2个list,第二遍可以试一下DFS

     1 class Solution(object):
     2     def levelOrderBottom(self, root):
     3         if not root:
     4             return []
     5         result = []
     6         prev = [root]
     7         cur = []
     8         
     9         while prev:
    10             for elem in prev:
    11                 if elem.left:
    12                     cur.append(elem.left)
    13                 if elem.right:
    14                     cur.append(elem.right)
    15             result.append([e.val for e in prev])
    16             prev, cur = cur, []
    17         result.reverse()
    18         return result

    2/16/2017, Java

    错误长的都不知道该怎么写了,而且这段代码效率低得可以不看了

    1. List, ArrayList, LinkedList, Queue, Stack的各种区别

    2. Interface, Implementing Class的区别,关系

    3. List<List<Integer>> ret = new ArrayList<>(); 语法上这是怎么一回事

    4. stack(level); level.clear();之后stack里面都是空List,这让用Python的人怎么活。所以Java是怎么存储数据的!

    5. 效率提高

     1 public class Solution {
     2     public List<List<Integer>> levelOrderBottom(TreeNode root) {
     3 
     4         Queue<TreeNode> current = new LinkedList<TreeNode>();
     5         TreeNode p = new TreeNode(-1);
     6         List<List<Integer>> ret = new ArrayList<>();
     7         List<Integer> level = new ArrayList<Integer>();
     8         Stack<List<Integer>> stack = new Stack<>();
     9         int arraySize = 0;
    10         
    11         if (root == null) return ret;
    12 
    13         current.add(root);
    14         while(current != null && !current.isEmpty()) {
    15             arraySize = current.size();
    16             for(int i = 0; i < arraySize; i++) {
    17                 p = current.poll();
    18                 if (p.left != null) {
    19                     current.add(p.left);
    20                 }
    21                 if (p.right != null) {
    22                     current.add(p.right);
    23                 }
    24                 level.add(p.val);
    25             }
    26             stack.push(level);
    27             level = new ArrayList<Integer>();
    28         }
    29         while(!stack.empty()) {
    30             ret.add(stack.pop());
    31         }
    32         return ret;
    33     }
    34 }
  • 相关阅读:
    27:单词翻转
    c++自制锁机程序--两行代码
    19:字符串移位包含问题
    18:验证子串
    23:二维数组回形遍历
    Java获取程序或项目路径的常用方法
    java 、Android 提交参数转码问题
    java 上传图片 打水印
    使用getGenericSuperclass()和getActualTypeArguments()将DAO做成泛型
    zoj 1010 Area【线段相交问题】
  • 原文地址:https://www.cnblogs.com/panini/p/5597287.html
Copyright © 2020-2023  润新知