103. Zigzag level order traversal of binary tree

103. Zigzag level order traversal of binary tree

Original title link:

103. Zigzag level order traversal of binary tree
https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/

Completion:

insert image description here

Problem-solving ideas:

The difficulty of this question is that one odd and one even, normally don't worry about it, but if it's reversed, then you have to pay attention.

  • When it is normal and get the normal sequence, you
    have to read it backwards at this time, and you can reverse the sequence, but the reverse sequence cannot guarantee that it will be the normal sequence next time.

So the solution is to add them in positive order, but each time the flag is in reverse order, let the next added element flip.

Reference Code:

package 西湖算法题解___中等题;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class __103二叉树的锯齿形层序遍历02 {
    
    
	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
    
    

	}

	/**
	 *
	 */
	public class TreeNode {
    
    
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode() {
    
    }
		TreeNode(int val) {
    
     this.val = val; }
		TreeNode(int val, TreeNode left, TreeNode right) {
    
    
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}

	public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    
    
		List<List<Integer>> res = new ArrayList<List<Integer>>();
		if (root == null){
    
    
			return res;
		}
		Queue<TreeNode> queue_adq = new ArrayDeque<>();
		queue_adq.add(root);
		boolean flag = false;//记录顺序
		while (!queue_adq.isEmpty()){
    
    
			List<Integer> arrayList =  new ArrayList<>();
			int levelsize = queue_adq.size();
			for (int i=0;i<levelsize;i++){
    
    
				TreeNode node = queue_adq.poll();
				if (node.left != null){
    
    
					queue_adq.offer(node.left);
				}
				if (node.right != null){
    
    
					queue_adq.offer(node.right);
				}
				if (flag){
    
    
					arrayList.add(0,node.val);  //每次将获取的值,加到最前面
				}else {
    
    
					arrayList.add(node.val);
				}
			}
			flag=!flag;
			res.add(arrayList);
		}
		return res;
	}


}

Guess you like

Origin blog.csdn.net/weixin_43554580/article/details/131761000