• LeetCode刷题笔记:165.输出二叉树


    1. 问题描述

    给你一棵二叉树的根节点 root ,请你构造一个下标从 0 开始、大小为 m x n 的字符串矩阵 res ,用以表示树的 格式化布局 。构造此格式化布局矩阵需要遵循以下规则:

    • 树的 高度 为 height ,矩阵的行数 m 应该等于 height + 1 。
    • 矩阵的列数 n 应该等于 2height+1 - 1 。
    • 根节点 需要放置在 顶行 的 正中间 ,对应位置为 res[0][(n-1)/2] 。
    • 对于放置在矩阵中的每个节点,设对应位置为 res[r][c] ,将其左子节点放置在 res[r+1][c-2height-r-1] ,右子节点放置在 res[r+1][c+2height-r-1] 。
    • 继续这一过程,直到树中的所有节点都妥善放置。
    • 任意空单元格都应该包含空字符串 “” 。

    返回构造得到的矩阵 res 。

    2. 解题思路

    ① 层序遍历获取二叉树的高度 height
    ② DFS深度优先遍历整个二叉树(二分法进行递归)

    3. 代码实现

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        private int treeHeight(TreeNode root) {
            int height = 0;
            Deque<TreeNode> q = new LinkedList<>();
            q.offer(root);
            while (!q.isEmpty()) {
                int n = q.size();
                for (int i = 0; i < n; i++) {
                    TreeNode temp = q.poll();
                    if (temp.left != null) {
                        q.offer(temp.left);
                    }
                    if (temp.right != null) {
                        q.offer(temp.right);
                    }
                }
                height++;
            }
            return height;
        }
    
        private void DFS(TreeNode root, List<List<String>> res, int height, int left, int right) {
            if (root == null) {
                return;
            }
            int mid = (left + right) / 2;
            res.get(height).set(mid, String.valueOf(root.val));
            DFS(root.left, res, height + 1, left, mid - 1);
            DFS(root.right, res, height + 1, mid + 1, right);
        }
    
        public List<List<String>> printTree(TreeNode root) {
            int m = treeHeight(root);
            int n = (int) Math.pow(2, m) - 1;
            List<List<String>> res = new ArrayList<>(m);
            for (int i = 0; i < m; i++) {
                List<String> temp = new ArrayList<>(n);
                for (int j = 0; j < n; j++) {
                    temp.add("");
                }
                res.add(temp);
            }
            DFS(root, res, 0, 0, n - 1);
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    容错限流框架之Hystrix上
    [附源码]SSM计算机毕业设计“云味坊”购物网站JAVA
    linux三剑客(grep、sed、awk)基本使用
    2023年工单管理系统排行榜单
    旷视 CEO 印奇被敲诈勒索:不给 300 万就出售公司敏感信息!
    国民技术N32G031系列单片机的AD采样
    长短期记忆网络(LSTM)原理解析
    Redis夺命十二问,你能扛到第几问?
    安装包 amd,amd64, arm,arm64 都有什么区别
    DBOW概要理解与记录
  • 原文地址:https://blog.csdn.net/hutianle/article/details/126466298