码迷,mamicode.com
首页 > 其他好文 > 详细

107. Binary Tree Level Order Traversal II

时间:2016-05-18 09:12:47      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 107. Binary Tree Level Order Traversal II 
* 11.21 By Mingyang I的解答加一行: * Collections.reverse(results);就好了
*/ public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> results = new ArrayList<List<Integer>>(); ArrayList<Integer> result = new ArrayList<Integer>(); if (root == null) return results; LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); int currentNumber = 1; int nextNumber = 0; while (queue.size() != 0) { TreeNode temp = queue.poll(); result.add(temp.val); currentNumber--; if (temp.left != null) { queue.add(temp.left); nextNumber++; } if (temp.right != null) { queue.add(temp.right); nextNumber++; } if (currentNumber == 0) { results.add(result); currentNumber = nextNumber; nextNumber = 0; result = new ArrayList<Integer>(); } } Collections.reverse(results); return results; }

 

107. Binary Tree Level Order Traversal II

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5503933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!