V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Acceml
V2EX  ›  程序员

[Leetcode] 103. 二叉树的锯齿形层次遍历

  •  
  •   Acceml ·
    Acceml · 2019-02-27 08:27:07 +08:00 · 1740 次点击
    这是一个创建于 1856 天前的主题,其中的信息可能已经有所发展或是发生改变。

    题目

    给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

    例如: 给定二叉树 [3,9,20,null,null,15,7],

        3
       / \
      9  20
        /  \
       15   7
    

    返回锯齿形层次遍历如下:

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

    题解

    这道题要求用 z 字型,就是要求知道深度。因为知道深度我们就可以根据深度的奇偶来判断如何打印。 首先相到的就是层序遍历,然后记录是第几层。层序遍历用队列的代码我们已经很熟悉了。

    /**
     * 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 LinkedList<>();
            if (root == null) {
                return res;
            }
            LinkedList<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            int depth = 0;
            while (!queue.isEmpty()) {
                int size = queue.size();
                LinkedList<Integer> currentRes = new LinkedList<>();
                // 当前层一直出队.
                while (size > 0) {
                    TreeNode current = queue.poll();
                    TreeNode left = current.left;
                    TreeNode right = current.right;
                    if (left != null) {
                        queue.add(left);
                    }
                    if (right != null) {
                        queue.add(right);
                    }
                    // 奇数层,从头添加; 偶数层从尾部添加.
                    if (depth % 2 != 0) {
                        currentRes.add(0, current.val);
                    } else {
                        currentRes.add(current.val);
                    }
                    size--;
                }
                // 把当前层遍历的结果加入到结果中.
                res.add(currentRes);
                depth++;
            }
            return res;
        }
    }
    

    同之前一样,我们想一想有没有递归的解法. 我们可以采用先序遍历的方式,先遍历节点,然后递归的遍历左子树和右子树。 稍作改动的是,需要在遍历左子树和右子树的时候带上深度的信息,才能知道是加在列表头还是列表尾部。 递归的结束条件就是遇到空节点。

    /**
     * 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 LinkedList<>();
            if (root == null) {
                return res;
            }
            helper(res, root, 0);
            return res;
        }
    
        public void helper(List<List<Integer>> res,TreeNode root, int depth) {
            if (root == null) {
                return;
            }
            // 注意这里 new List, 说明当前节点递归进入了新的层.
            if (res.size() <= depth) {
                res.add(new LinkedList<>());
            }
            
            if (depth % 2 != 0) {
                res.get(depth).add(0, root.val);
            } else {
                res.get(depth).add(root.val);
            }
    
            helper(res, root.left, depth + 1);
            helper(res, root.right, depth + 1);
        }
    }
    

    热门阅读

    Leetcode 名企之路

    5 条回复    2019-03-04 20:29:18 +08:00
    balamiao
        1
    balamiao  
       2019-02-27 09:27:41 +08:00
    哈哈,前几天面试遇到这个问题,我用的就是第二种递归的方式,但面试官表示不太满意,他希望看到的还是第一种~
    HunterPan
        2
    HunterPan  
       2019-02-27 09:33:51 +08:00
    为什么要用递归?
    zcjfesky
        3
    zcjfesky  
       2019-02-27 11:23:20 +08:00
    输入如果是这样的二叉树数据 (共 2^n-1 个点,反推出层数为 n),可以直接两层循环输出?

    for i from 1 to n
    if i 为奇数 则将 s[2^(i-1)]~s[2^i-1] 顺序输出;
    else 将 s[2^(i-1)]~s[2^i-1] 逆序输出

    是我哪里理解错了吗?
    hasaki1997
        4
    hasaki1997  
       2019-02-27 16:03:13 +08:00 via Android
    bfs 也可以吧,稍微处理下就行吧
    Acceml
        5
    Acceml  
    OP
       2019-03-04 20:29:18 +08:00
    @balamiao 面试官面试你之前可能只知道第一种解法。递归他觉得不行。嘻嘻~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5174 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:42 · PVG 17:42 · LAX 02:42 · JFK 05:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.