binary tree zigzag traversal

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

Line 51: error: illegal start of type
 
 
 
 
 
 

class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<List<TreeNode>>(); if(root == null){ return result; } Stack<TreeNode> s1 = new Stack<TreeNode>(); Stack<TreeNode> s2 = new Stack<TreeNode>(); s1.push(root); while(!s1.isEmpty() || !s2.isEmpty()){ List<Integer> tmp = new ArrayList<Integer>(); while(!s1.isEmpty()){ TreeNode current = s1.pop(); tmp.add(current.val); if(current.left != null){ s2.push(current.left); } if(current.right != null){ s2.push(current.right); } } result.add(tmp); List<Integer> tmp = new ArrayList<Integer>(); while(!s2.isEmpty()){ TreeNode current = s2.pop(); tmp.add(current.val); if(current.right != null){ s1.push(current.right); } if(current.left != null){ s1.push(current.left); } } if(!tmp.isEmpty()){ result.add(tmp); } } } return result; }

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9458362.html