103 Title: zigzag traverse the level binary tree

One. Problem Description

Given a binary tree, the node returns the value of its level zigzag traversal. (I.e., start right left, then right to left for the next traverse floor, and so on, alternating between the layers).

E.g:

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

    3

   /  \

  9  20

      /  \

   15   7

Back zigzag traverse the level as follows:

[

  [3],

  [20,9],

  [15,7]

]

two. Problem-solving ideas

The title ideas: recursive breadth-first traversal method to solve +

Step a: Construction traversal function (global variables stored data list, data stored in the local variables of the current number of nodes, determining the amount of label mark traversing from left to right, or right to left traversing)

Step two: for traversal sequence in a recursive function, when faced with odd number of layers, data from left to right traversal of the data obtained data, in the list, and the respective sub-tree from left to right to the new data stored in the memory and the flag mark = -1;

Step three: conduct traversal sequence in a recursive function, when faced with an even number of layers, data from right to left traversal of the data obtained the data in the list, and the respective sub-tree from left to right to the new data stored in the memory and the flag mark = -1;

Step Four: returns to step two when the data is not empty, return to the list when the list is empty.

three. Results of the

When execution: 1 ms, defeated 100.00% of users in all java submission

Memory consumption: 36 MB, defeated 40.11% of all users in java submission

four. Java code

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        
        List<List<Integer>> list=new ArrayList<List<Integer>>();
        if(root==null) {
            return list;
        }
        List<TreeNode> data=new ArrayList<TreeNode>();
        data.add(root);
        treeNo(list,data,1);
        return list;
    }
    public void treeNo(List<List<Integer>> list,List<TreeNode> data,int mark) {
        if(data.size()==0) {
            return;
        }
        List<TreeNode> tempdata=new ArrayList<TreeNode>();
        List<Integer> temp=new ArrayList<Integer>();
        if(mark==1) {    
            for(int i=0;i<data.size();i++) {
                temp.add(data.get(i).val);
                if(data.get(i).left!=null) {
                    tempdata.add(data.get(i).left);
                }
                if(data.get(i).right!=null) {
                    tempdata.add(data.get(i).right);
                }
            }
            mark=-1;
        }else {
            for(int i=data.size()-1,j=0;i>=0||j<data.size();i--,j++) {
                temp.add(data.get(i).val);
                if(data.get(j).left!=null) {
                    tempdata.add(data.get(j).left);
                }
                if(data.get(j).right!=null) {
                    tempdata.add(data.get(j).right);
                }
            }    
            mark=1;
        }
        list.add(temp);
        treeNo(list,tempdata,mark);
    }
}

 

Guess you like

Origin www.cnblogs.com/xiaobaidashu/p/11809531.html