Likou——Analysis of Zigzag Hierarchical Traversal of Binary Tree

Title description

Given a binary tree, return the zigzag hierarchical traversal of its node values. (That is, the next layer is traversed from left to right, then from right to left, and so on, alternating between layers).

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

3

/
920
/
157
returns a zigzag traverse the level as follows:

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

Thinking analysis:

First of all, this topic is how to traverse. What I use is dfs to traverse the tree, and then cleverly use the id tag number, which represents which layer the node should belong to, and then use it every time. Compare it with list.size. If it is smaller than it, it proves that it and the existence of a sibling node. Only use that layer to add its value. On the contrary, if the id is greater than or equal to list.size, it means that it is the first one. When we reach the node of this layer, we add a new list to the list, and then save the value of node. Obviously, after doing this, all the results obtained are the results of traversing the left and then traversing the right. The topic requires that we want each The traversal order of the layers is different, and the left and right are rotated. Then we can completely reverse and save all the even-numbered layers of the list after getting the result, and then we can get the result required by the title, and the final code execution efficiency is even less. Higher than the official offer.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    
    
		List<List<Integer>> list = new LinkedList<List<Integer>>();
		List<Integer> temp = new LinkedList<Integer>();
		list.add(temp);
		list = work(root,list,0);
		int k = 0;
		for(List<Integer> res:list) {
    
    
			k++;
			if(k % 2 == 0) 
				Collections.reverse(res);
		}
		return list;
    }
	public List<List<Integer>> work(TreeNode node,List<List<Integer>> list,int id) {
    
    
		if(id >= list.size()) {
    
    
			List<Integer> temp = new LinkedList<Integer>();
			temp.add(node.val);
			list.add(temp);
		}else {
    
    
			list.get(id).add(node.val);
		}
		if(node.left != null) {
    
    
			work(node.left,list,id+1);
		}
		if(node.right != null) {
    
    
			work(node.right,list,id+1);
		}
		return list;
    }
}/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    
    
		List<List<Integer>> list = new LinkedList<List<Integer>>();
		if(root == null)
			return list;
		List<Integer> temp = new LinkedList<Integer>();
		list.add(temp);
		list = work(root,list,0);
		int k = 0;
		for(List<Integer> res:list) {
    
    
			k++;
			if(k % 2 == 0) 
				Collections.reverse(res);
		}
		return list;
    }
	public List<List<Integer>> work(TreeNode node,List<List<Integer>> list,int id) {
    
    
		if(id >= list.size()) {
    
    
			List<Integer> temp = new LinkedList<Integer>();
			temp.add(node.val);
			list.add(temp);
		}else {
    
    
			list.get(id).add(node.val);
		}
		if(node.left != null) {
    
    
			work(node.left,list,id+1);
		}
		if(node.right != null) {
    
    
			work(node.right,list,id+1);
		}
		return list;
    }
}

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/109595344