Leecode 103 Binary Tree Zigzag Level Order Traversal Z型层次遍历二叉树,外加正常的层次遍历

首先应当知道java中LinkedList既ke可以充当队列,也可充当栈。既让是Z型的话,选用LinkedListgeng更方便。

题目如下:

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

第一种解决办法如下:

package test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LC103Try3
{
	public List> zigzagLevelOrder(TreeNode root)
	{
		List> ret = new ArrayList>();
		getPass(root,ret,0);
		return ret;
	}

	public void getPass(TreeNode root,List> ret,int level){
		if(root == null){
			return;
		}
		//这一步还容易错
		if(ret.size()-1());
		}
		//linkedList可以充当队列,也可以充当栈它有addFirst方法
		if(level%2==1){
			((LinkedList)ret.get(level)).addFirst(root.val);
		}else{
			ret.get(level).add(root.val);
		}
		getPass(root.left,ret,level+1);
		getPass(root.right,ret,level+1);
	}
}


第二种解决办法:稍微慢一点

package test;

import java.util.ArrayList;
import java.util.List;
//Z型层次遍历
public class LC103Try2
{
	public List> zigzagLevelOrder(TreeNode root)
	{
		List> ret = new ArrayList>();
		if (root == null)
		{
			return ret;
		}
		List lastList = new ArrayList();
		List thislist = new ArrayList();
		lastList.add(root);
		int deep = 0;
		List listval = new ArrayList();
		while (lastList.size() > 0)
		{
			if (deep % 2 == 0)
			{
				for (int i = lastList.size() - 1; i >= 0; i--)
				{
					TreeNode node = lastList.get(i);
					listval.add(node.val);

					if (node.left != null)
					{
						thislist.add(node.left);
					}
					if (node.right != null)
					{
						thislist.add(node.right);
					}

				}
			}else{
				for (int i = lastList.size() - 1; i >= 0; i--)
				{
					TreeNode node = lastList.get(i);
					listval.add(node.val);

					if (node.right != null)
					{
						thislist.add(node.right);
					}
					if (node.left != null)
					{
						thislist.add(node.left);
					}

				}
				
			}
			
			deep++;
			ret.add(listval);
			listval = new ArrayList();
			lastList = thislist;
			thislist = new ArrayList();
		}

		return ret;
	}

	public void getPass(TreeNode root, List> ret,
			List lastList, List thislist)
	{

	}

}

正常的层次遍历:

第一种办法

package test;

import java.util.ArrayList;
import java.util.List;
//正常的层次遍历
public class LC103Try1
{
    public List> zigzagLevelOrder(TreeNode root) {
    	List> ret= new ArrayList>();
    	if(root==null){
    		return ret;
    	}
    	List list= new ArrayList();
    	List listval=new ArrayList();
    	list.add(root);
    	int index=0;    	
    	for(int start=0;start();    			
    			index=list.size()-1;    			   			
    		}
    	}
    	
    	return ret;
    }
   

}


 

第二种办法:

package test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LC103Try12
{
	public List> zigzagLevelOrder(TreeNode root)
	{
		List> ret = new ArrayList>();
		getPass(root,ret,0);
		return ret;
	}

	public void getPass(TreeNode root,List> ret,int level){
		if(root == null){
			return;
		}
		//这一步还容易错
		if(ret.size()-1());
		}
		
		ret.get(level).add(root.val);
		
		getPass(root.left,ret,level+1);
		getPass(root.right,ret,level+1);
	}

}

哈哈

你可能感兴趣的:(leetcode)