《剑指 Offer (第 2 版)》第 32-3 题:之字形打印二叉树

第 32-3 题:之字形打印二叉树

传送门:之字形打印二叉树,牛客网 online judge 地址。

请实现一个函数按照之字形顺序从上向下打印二叉树。

即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。

样例:

输入如下图所示二叉树 [8, 12, 2, null, null, 6, 4, null, null, null, null]

     8
    / \
  12  2
      / \
     6   4

输出:[[8], [2, 12], [6, 4]]

思路:设置一个向左向右的变量更换当前层元素的插入方式即可。

Python 代码:

# 样例
# 输入如下图所示二叉树[8, 12, 2, null, null, 6, 4, null, null, null, null]
#     8
#    / \
#   12  2
#      / \
#     6   4
# 输出:[[8], [2, 12], [6, 4]]

# Definition for a binary tree node.


class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution(object):
    def printFromTopToBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if root is None:
            return []
        queue = [root]
        res = []
        turn_left = True
        while queue:
            cur_list = []
            size = len(queue)
            for _ in range(size):
                top = queue.pop(0)
                if turn_left:
                    cur_list.append(top.val)
                else:
                    cur_list.insert(0, top.val)
                if top.left:
                    queue.append(top.left)
                if top.right:
                    queue.append(top.right)
            res.append(cur_list)
            turn_left = not turn_left
        return res

Java 代码:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    public ArrayList> Print(TreeNode pRoot) {
        ArrayList> res = new ArrayList<>();
        if (pRoot == null) {
            return res;
        }
        LinkedList queue = new LinkedList<>();
        queue.addLast(pRoot);
        boolean right = true;
        while (!queue.isEmpty()) {
            int size = queue.size();
            // 之 字形 一开始向右
            ArrayList curLevel = new ArrayList<>();
            Stack stack = new Stack<>();
            for (int i = 0; i < size; i++) {
                if (right) {
                    TreeNode node = queue.removeFirst();
                    curLevel.add(node.val);
                    if (node.left != null) {
                        stack.add(node.left);
                    }
                    if (node.right != null) {
                        stack.add(node.right);
                    }
                } else {
                    TreeNode node = queue.removeFirst();
                    curLevel.add(node.val);
                    if (node.right != null) {
                        stack.add(node.right);
                    }
                    if (node.left != null) {
                        stack.add(node.left);
                    }
                }
            }
            while (!stack.isEmpty()) {
                queue.addLast(stack.pop());
            }
            right = !right;
            res.add(curLevel);
        }
        return res;
    }

}

你可能感兴趣的:(《剑指 Offer (第 2 版)》第 32-3 题:之字形打印二叉树)