Binary Tree Level Order Traversal II

原创 2017年01月03日 16:26:13

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>();
		nodeList.add(root);
		if(root == null){
			return 	lsts;
		}
		index = 0;
		while(t != 0){
            if(f == t){
            	t = cnt;
            	f = 0;
            	cnt = 0;
            	lsts.add(0,lst);
            	lst = new ArrayList<Integer>();
            }
            else{
            	TreeNode node = nodeList.get(index ++);
            	if(node != null){
            		f ++;
            		lst.add(node.val);
            	}
            	if(node.left != null){
            		cnt ++;
            		nodeList.add(node.left);
            	}
            	if(node.right != null){
            		cnt ++;
            		nodeList.add(node.right);
            	}
            }
		}
	    return 	lsts;
    }
}

然而只占胜了23.52%,于是我们选择还是按顺序放入链表,在进行反转。

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>();
		nodeList.add(root);
		if(root == null){
			return 	lsts;
		}
		index = 0;
		while(t != 0){
            if(f == t){
            	t = cnt;
            	f = 0;
            	cnt = 0;
            	lsts.add(lst);
            	lst = new ArrayList<Integer>();
            }
            else{
            	TreeNode node = nodeList.get(index ++);
            	if(node != null){
            		f ++;
            		lst.add(node.val);
            	}
            	if(node.left != null){
            		cnt ++;
            		nodeList.add(node.left);
            	}
            	if(node.right != null){
            		cnt ++;
            		nodeList.add(node.right);
            	}
            }
		}
		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;
    }
}

此刻战胜了69.49% 。当然了,自然是可以利用递归解决的,递归也可以解决的。

        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
            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()) {
                list.add(0, new LinkedList<Integer>());
            }
            levelMaker(list, root.left, level+1);
            levelMaker(list, root.right, level+1);
            list.get(list.size()-level-1).add(root.val);
        }

这个方案战胜了69.49%


相关文章推荐

[LeetCode]Binary Tree Level Order Traversal II(JAVA)

这道题我真的是使出了洪荒之力啊 /** * Definition for a binary tree node. * public class TreeNode { * int...
  • jsh0123
  • jsh0123
  • 2016年09月14日 10:32
  • 162

【LeetCode】Binary Tree Level Order Traversal II 解题报告

Binary Tree Level Order Traversal II [LeetCode] Given a binary tree, return the bottom-up level orde...

[leetcode] 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...

leetcode 之Binary Tree Level Order Traversal I和II 解题思路

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from lef...

[LeetCode]--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...

LeetCode 107. Binary Tree Level Order Traversal II

107. Binary Tree Level Order Traversal IIDescription Given a binary tree, return the bottom-up leve...

LeetCode(107)Binary Tree Level Order Traversal II

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left t...

Binary Tree Level Order Traversal II

/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; ...

LeetCode 107. Binary Tree Level Order Traversal II 树的BFS、DFS

Binary Tree Level Order Traversal II 题意 思路 代码 BFS DFS107. Binary Tree Level Order Traversal II Giv...

LeetCode 之 Binary Tree Level Order Traversal II

原题: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Tree Level Order Traversal II
举报原因:
原因补充:

(最多只允许输入30个字)