首页 > 学院 > 开发设计 > 正文

leetcode--107. Binary Tree Level Order Traversal II

2019-11-14 11:37:06
字体:
来源:转载
供稿:网友

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).

题解

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); List<List<Integer>> ans = new LinkedList<>(); if(root == null) return ans; queue.offer(root); while(!queue.isEmpty()){ int levelNum = queue.size(); List<Integer> tmpList = new LinkedList<>(); for(int i = 0; i < levelNum; ++i){ if(queue.peek().left != null) queue.offer(queue.peek().left); if(queue.peek().right != null) queue.offer(queue.peek().right); tmpList.add(queue.poll().val); } ans.add(0, tmpList); } return ans; }}
上一篇:vs2010忽略警告

下一篇:python lambda用法

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表