关闭

Binary Tree Level Order Traversal II

标签: Level Order TraversaTree TraversalLeetCode
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>();
		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%


0
0

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