如何用二维数组保存二叉树层次遍历结果?

题目链接:https://www.nowcoder.com/study/live/716/5/17

给定一个二叉树,返回该二叉树层序遍历的结果,(从左到右,一层一层地遍历)
例如:
给定的二叉树是{3,9,20,#,#,15,7},
如何用二维数组保存二叉树层次遍历结果?_第1张图片

该二叉树层序遍历的结果是
[
[3],
[9,20],
[15,7]

]

提示:
0 <= 二叉树的结点数 <= 1500

示例1
输入
{1,2}
输出
[[1],[2]]
示例2
输入
{1,2,3,4,#,#,5}
输出
[[1],[2,3],[4,5]]

题解

  1. 由于题目要求,输出结果中,每一层对应一个 ArrayList,因此需要对传统的层次遍历算法进行一点点小改动,需要在遍历时反映层数。
  2. 需要记录每个节点的层数,因此我们使用两个队列。一个队列保存父节点,另外一个队列保存对应的层数。
  3. 只记录每个节点的层数,是无法实现题目要求的输出。我们需要记录每层的输出,然后在每层结束之后将其 add 到总 ArrayList 中。这里的难点在于如何判断一层的结束。
  4. 如何判断一层结束(方法一):我使用一个变量记录当前遍历的层数。正常遍历时,满足parent 的层数 + 1 == 当前遍历的层数。一旦该条件不满足,则代表 parent 层数发生变化,也就是说,当前遍历的层数需要发生变化。于是在这时更新 “当前遍历层数” 为 parent 层数 + 1。由此感知层数发生变化。
  5. 如何判断一层结束(方法二):记录相邻两个 parent 结点的层数,若不一致则层数发生变化。
  6. 如何判断一层结束(方法三):每个 while 循环,让一整层的父节点出队,而不是只让一个父节点出队。由此一次 while 对应着一层,无需记录当前层数。

实现

方法一代码:

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @return int整型ArrayList>
     */
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        if (root == null) {
            return new ArrayList<ArrayList<Integer>>();
        }
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        ArrayList<Integer> curLevelRes = new ArrayList<>();
        int curLevel = 1;
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        Queue<Integer> layerQueue = new LinkedList<>();

        curLevelRes.add(root.val);
        nodeQueue.add(root);
        layerQueue.add(curLevel);

        while (!nodeQueue.isEmpty()) {
            TreeNode parent = nodeQueue.poll();
            Integer parentLayer = layerQueue.poll();

            if (parentLayer + 1 != curLevel) { // curLevel change
                curLevel = parentLayer + 1;
                res.add(curLevelRes);
                curLevelRes = new ArrayList<>();
            }
            
            TreeNode left = parent.left;
            TreeNode right = parent.right;
            if (left != null) {
                curLevelRes.add(left.val);
                nodeQueue.add(left);
                layerQueue.add(curLevel);
            }
            if (right != null) {
                curLevelRes.add(right.val);
                nodeQueue.add(right);
                layerQueue.add(curLevel);
            }
        }

        return res;
    }
}

方法二代码:

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */
public class Solution {
    /**
     *
     * @param root TreeNode类
     * @return int整型ArrayList>
     */
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        if (root == null) {
            return new ArrayList<ArrayList<Integer>>();
        }
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        ArrayList<Integer> curLevelRes = new ArrayList<>();
        int lastParentLayer = 0;
        int curParentLayer = 0;
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        Queue<Integer> layerQueue = new LinkedList<>();

        curLevelRes.add(root.val);
        nodeQueue.add(root);
        layerQueue.add(curParentLayer + 1);

        while (!nodeQueue.isEmpty()) {
            TreeNode parent = nodeQueue.poll();
            Integer parentLayer = layerQueue.poll();

            lastParentLayer = curParentLayer;
            curParentLayer = parentLayer;
            if (lastParentLayer != curParentLayer) { // curLevel change
                res.add(curLevelRes);
                curLevelRes = new ArrayList<>();
            }

            TreeNode left = parent.left;
            TreeNode right = parent.right;
            if (left != null) {
                curLevelRes.add(left.val);
                nodeQueue.add(left);
                layerQueue.add(curParentLayer + 1);
            }
            if (right != null) {
                curLevelRes.add(right.val);
                nodeQueue.add(right);
                layerQueue.add(curParentLayer + 1);
            }
        }
        return res;
    }
}

方法三代码:

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @return int整型ArrayList>
     */
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        Queue<TreeNode> parentQueue = new LinkedList<>();
        res.add(new ArrayList<>(Arrays.asList(root.val)));
        parentQueue.add(root);

        while (!parentQueue.isEmpty()) {
            ArrayList<Integer> curLevelList = new ArrayList<>();
            int size = parentQueue.size();
            // Take care: parentQueue.size() is not effectively final!
            for (int i = 0; i < size; i++) {

                TreeNode parent = parentQueue.poll();
                TreeNode left = parent.left;
                TreeNode right = parent.right;
                if (left != null) {
                    curLevelList.add(left.val);
                    parentQueue.add(left);
                }
                if (right != null) {
                    curLevelList.add(right.val);
                    parentQueue.add(right);
                }
            }

            // When traversing the last layer, it's empty.
            if (!curLevelList.isEmpty()) {
                res.add(curLevelList);
            }
        }
        return res;
    }
}

你可能感兴趣的:(算法,二叉树)