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

2019-02-27 08:27:07 +08:00
 Acceml

题目

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

例如: 给定二叉树 [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);
    }
}

热门阅读

1772 次点击
所在节点    程序员
5 条回复
balamiao
2019-02-27 09:27:41 +08:00
哈哈,前几天面试遇到这个问题,我用的就是第二种递归的方式,但面试官表示不太满意,他希望看到的还是第一种~
HunterPan
2019-02-27 09:33:51 +08:00
为什么要用递归?
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
2019-02-27 16:03:13 +08:00
bfs 也可以吧,稍微处理下就行吧
Acceml
2019-03-04 20:29:18 +08:00
@balamiao 面试官面试你之前可能只知道第一种解法。递归他觉得不行。嘻嘻~~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/539086

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX