# Binary Tree Level Order Traversal II

95人阅读 评论(0)

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]
]

public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
int cnt = 0;
int f = 0;
int t = 1;
int index;
List<List<Integer>> lsts = new ArrayList<List<Integer>>();
List<Integer> lst = new ArrayList<Integer>();
List<TreeNode> nodeList = new ArrayList<TreeNode>();
if(root == null){
return 	lsts;
}
index = 0;
while(t != 0){
if(f == t){
t = cnt;
f = 0;
cnt = 0;
lst = new ArrayList<Integer>();
}
else{
TreeNode node = nodeList.get(index ++);
if(node != null){
f ++;
}
if(node.left != null){
cnt ++;
}
if(node.right != null){
cnt ++;
}
}
}
return 	lsts;
}
}

public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
int cnt = 0;
int f = 0;
int t = 1;
int index;
List<List<Integer>> lsts = new ArrayList<List<Integer>>();
List<Integer> lst = new ArrayList<Integer>();
List<TreeNode> nodeList = new ArrayList<TreeNode>();
if(root == null){
return 	lsts;
}
index = 0;
while(t != 0){
if(f == t){
t = cnt;
f = 0;
cnt = 0;
lst = new ArrayList<Integer>();
}
else{
TreeNode node = nodeList.get(index ++);
if(node != null){
f ++;
}
if(node.left != null){
cnt ++;
}
if(node.right != null){
cnt ++;
}
}
}
int from = 0,to = lsts.size() - 1;
while(from < to){
List<Integer> tempF = lsts.get(from);
List<Integer> tempE = lsts.get(to);
lsts.set(from, tempE);
lsts.set(to, tempF);
from ++;
to --;
}
return 	lsts;
}
}

        public List<List<Integer>> levelOrderBottom(TreeNode root) {
levelMaker(wrapList, root, 0);
return wrapList;
}

public void levelMaker(List<List<Integer>> list, TreeNode root, int level) {
if(root == null) return;
if(level >= list.size()) {
}
levelMaker(list, root.left, level+1);
levelMaker(list, root.right, level+1);
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：42512次
• 积分：1045
• 等级：
• 排名：千里之外
• 原创：57篇
• 转载：44篇
• 译文：0篇
• 评论：3条
评论排行