• Binary Tree Zigzag Level Order Traversal


    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,#,#,15,7},

        3
       / 
      9  20
        /  
       15   7
    

    return its zigzag level order traversal as:

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

    先输出成arraylist,然后再反转即可。
     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
    12         // IMPORTANT: Please reset any member data you declared, as
    13         // the same Solution instance will be reused for each test case.
    14         ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
    15         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    16         if(root == null) return result;
    17         queue.add(root);
    18         TreeNode nl = new TreeNode(Integer.MIN_VALUE);
    19         queue.add(nl);
    20         ArrayList<Integer> cur = new ArrayList<Integer>();
    21         while(queue.size() != 1){
    22             TreeNode rn = queue.remove(0);
    23             if(rn.val == Integer.MIN_VALUE){
    24                 queue.add(nl);
    25                 result.add(cur);
    26                 cur = new ArrayList<Integer>();
    27             }
    28             else{
    29                 if(rn.left != null) queue.add(rn.left);
    30                 if(rn.right != null) queue.add(rn.right);
    31                 cur.add(rn.val);
    32             }
    33         }
    34         result.add(cur);
    35         for (int i=0; i<result.size(); i++)
    36             if (i % 2 == 1) reverse(result,i);
    37         return result;
    38     }
    39     public void reverse(ArrayList<ArrayList<Integer>> result, int num){
    40         int len = result.get(num).size();
    41         for(int i = 0; i < len / 2; i ++){
    42             int tmp = result.get(num).get(i);
    43             result.get(num).set(i, result.get(num).get(len - 1 - i));
    44             result.get(num).set(len - 1 - i, tmp);
    45         }
    46     }
    47 }

     第三遍:

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    12         ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
    13         if(root == null) return result;
    14         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
    15         queue.add(root);
    16         TreeNode nil = new TreeNode(Integer.MAX_VALUE);
    17         queue.add(nil);
    18         boolean even = false;
    19         ArrayList<Integer> row = new ArrayList<Integer>();
    20         while(queue.size() != 1){
    21             TreeNode tmp = queue.poll();
    22             if(tmp.val != Integer.MAX_VALUE){
    23                 row.add(tmp.val);
    24                 if(tmp.left != null) queue.add(tmp.left);
    25                 if(tmp.right != null) queue.add(tmp.right);
    26             } else {
    27                 if(even) reverseRow(row);
    28                 result.add(row);
    29                 row = new ArrayList<Integer>();
    30                 queue.add(nil);
    31                 even = !even;
    32             }
    33         }
    34         if(even) reverseRow(row);
    35         result.add(row);
    36         return result;
    37     }
    38     
    39     public void reverseRow(ArrayList<Integer> row){
    40         int len = row.size();
    41         for(int i = 0; i < len / 2; i ++){
    42             int tmp = row.get(i);
    43             row.set(i, row.get(len - 1 - i));
    44             row.set(len - 1 - i, tmp);
    45         }
    46     }
    47 }
  • 相关阅读:
    python模块总结(一)命令行解析模块argparse
    TCP(一)三次握手和四次挥手
    容器网络(四)vxlan
    容器网络(三)容器间通信
    kvm虚拟化(二)网络虚拟化
    KVM虚拟化(一)创建虚拟机
    数字操作 —— 9_ 回文数
    数字操作 —— 8_字符串转换整数(atoi)
    数字操作 —— 7_整数反转
    字符串操作 —— 763_划分字母区间
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3426338.html
Copyright © 2020-2023  润新知