zoukankan      html  css  js  c++  java
  • 103. 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,null,null,15,7],

        3
       / 
      9  20
        /  
       15   7
    

    return its zigzag level order traversal as:

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

    M1: BFS

    time: O(n), space: O(2^height)

    /**
     * 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>> res = new ArrayList<>();
            Queue<TreeNode> q = new LinkedList<>();
            if(root == null) {
                return res;
            }
            
            boolean reverse = false;
            q.offer(root);
            while(!q.isEmpty()) {
                List<Integer> level = new ArrayList<>();
                int size = q.size();
                for(int i = 0; i < size; i++) {
                    TreeNode tmp = q.poll();
                    if(reverse) {
                        level.add(0, tmp.val);
                    } else {
                        level.add(tmp.val);
                    }
                    if(tmp.left != null) {
                        q.offer(tmp.left);
                    }
                    if(tmp.right != null) {
                        q.offer(tmp.right);
                    }
                }
                reverse = !reverse;
                res.add(level);
            }
            return res;
        }
    }

    M2: recursion

    time: O(n), space: O(height)

    /**
     * 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>> res = new ArrayList<>();
            if(root == null) {
                return res;
            }
            levelOrder(root, 0, res);
            return res;
        }
        
        public void levelOrder(TreeNode root, int level, List<List<Integer>> res) {
            if(root == null) {
                return;
            }
            if(res.size() == level) {
                res.add(new ArrayList<>());
            }
            if(level % 2 == 0) {
                res.get(level).add(root.val);
            } else {
                res.get(level).add(0, root.val);
            }
            
            levelOrder(root.left, level + 1, res);
            levelOrder(root.right, level + 1, res);
        }
    }
  • 相关阅读:
    安卓上的蓝牙数据传输
    算法题 整理--- 不定时更新
    Scrapy 框架的安装以及常用指令
    Flask 基础
    HTTP 请求包含: (请求行,请求头,请求体)
    Python 字符串,列表,字典,集合的常用方法
    Vue+Django 登录测试
    用view视图不含序列化写图书管理系统
    解决hao123恶意捆绑浏览器问题
    不同试图实现增删改查操作
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10193632.html
Copyright © 2011-2022 走看看