关闭

Binary Tree Zigzag Level Order Traversal

56人阅读 评论(0) 收藏 举报
分类:

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

对树进行Z形的层次遍历。首先想到的是,把所有的节点放入链表中,当行数为奇数行时,对该链表的节点进行反转。

public class Solution {
    public List<List<Integer>> zigzagLevelOrder(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);
            	}
            }
		}
		for(int i = 0;i < lsts.size();i++){
		    if(i%2 == 1){
		        List<Integer> ls = lsts.get(i);
		        int from = 0;
		        int to = ls.size()-1;
		        while(from < to){
		            Integer temp = ls.get(from);
		            ls.set(from,ls.get(to));
		            ls.set(to,temp);
		            from ++;
		            to --;
		        }
		    }
		}
	    return 	lsts;
    }
}
战胜了42.51,反转节点显然效率应该不是最高的。如果不采取上述的想法,我们能不能直接存入呢,当奇数层我们节点从左到右放入字节点,当为偶数层时,从右到左放入节点。

public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        int cnt = 0;
		int f = 0;
		int t = 1;
		int index; 
		int flag = 1;
		int top = 0;
		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);
            	index =top + t;
            	top = index;
            	lst = new ArrayList<Integer>();
            	if(flag == 1){
            		flag = 2;
            	}else{
            		flag = 1;
            	}
            }
            else{
            	TreeNode node = nodeList.get(index--);
            	if(node != null){
            		f ++;
            		lst.add(node.val);
            	}
            	if(flag == 1){
	            	if(node.left != null){
	            		cnt ++;
	            		nodeList.add(node.left);
	            	}
	            	if(node.right != null){
	            		cnt ++;
	            		nodeList.add(node.right);
	            	}
            	}else{
            		if(node.right != null){
	            		cnt ++;
	            		nodeList.add(node.right);
	            	}
	            	if(node.left != null){
	            		cnt ++;
	            		nodeList.add(node.left);
	            	}
            	}
            }
		}

	    return 	lsts;
    }
}

此刻战胜了92.03%

当然啦,可以递归解决的。参考大神方案

public List<List<Integer>> zigzagLevelOrder(TreeNode root) 
    {
        List<List<Integer>> sol = new ArrayList<>();
        travel(root, sol, 0);
        return sol;
    }
    
    private void travel(TreeNode curr, List<List<Integer>> sol, int level)
    {
        if(curr == null) return;
        
        if(sol.size() <= level)
        {
            List<Integer> newLevel = new LinkedList<>();
            sol.add(newLevel);
        }
        
        List<Integer> collection  = sol.get(level);
        if(level % 2 == 0) collection.add(curr.val);
        else collection.add(0, curr.val);
        
        travel(curr.left, sol, level + 1);
        travel(curr.right, sol, level + 1);
   }

效率还可以,战胜了92.09%。

0
0

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