数据结构与算法之LeetCode-655. 输出二叉树 - 力扣(LeetCode)

news/2024/7/5 23:22:52

655. 输出二叉树 - 力扣(LeetCode)

  • 先构建高度 height
  • 在构建层数,根据高度和层数得到数的位置
DFS
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string[][]}
 */
var printTree = function(root) {
    const calDepth = (root) => {
        let h = 0;
        if (root.left) {
            h = Math.max(h, calDepth(root.left) + 1);
        }
        if (root.right) {
            h = Math.max(h, calDepth(root.right) + 1);
        }
        return h;
    }

    const dfs = (res, root, r, c, height) => {
        res[r][c] = root.val.toString();
        if (root.left) {
            dfs(res, root.left, r + 1, c - (1 << (height - r - 1)), height);
        }
        if (root.right) {
            dfs(res, root.right, r + 1, c + (1 << (height - r - 1)), height);
        }
    }

    const height = calDepth(root);
    const m = height + 1;
    const n = (1 << (height + 1)) - 1;
    const res = new Array(m).fill(0).map(() => new Array(n).fill(''));
    dfs(res, root, 0, Math.floor((n - 1) / 2), height);
    return res;
};

执行结果:通过

执行用时:64 ms, 在所有 JavaScript 提交中击败了34.38%的用户

内存消耗:41.4 MB, 在所有 JavaScript 提交中击败了87.50%的用户

通过测试用例:73 / 73

BFS
var printTree = function(root) {
    const height = CalDepth(root);
    const m = height + 1;
    const n = (1 << (height + 1)) - 1;
    const res = new Array(m).fill(0).map(() => new Array(n).fill(''));
    const queue = [];
    queue.push([root, 0, Math.floor((n - 1) / 2)]);
    while (queue.length > 0) {
        const t = queue.shift();
        const node = t[0];
        let r = t[1], c = t[2];
        res[r][c] = node.val.toString();
        if (node.left) {
            queue.push([node.left, r + 1, c - (1 << (height - r - 1))]);
        }
        if (node.right) {
            queue.push([node.right, r + 1, c + (1 << (height - r - 1))]);
        }
    }
    return res;
};

const CalDepth = (root) => {
    let res = -1;
    const queue = [root];
    while (queue.length > 0) {
        let len = queue.length;
        res++;
        while (len > 0) {
            len--;
            const t = queue.shift();
            if (t.left) {
                queue.push(t.left);
            }
            if (t.right) {
                queue.push(t.right);
            }
        }
    }
    return res;
}

参考链接

655. 输出二叉树 - 力扣(LeetCode)

输出二叉树 - 输出二叉树 - 力扣(LeetCode)

[[Python/Java/TypeScript/Go] DFS - 输出二叉树 - 力扣(LeetCode)](


http://www.niftyadmin.cn/n/4426632.html

相关文章

物联网在教育领域应用前景广阔

十年树木&#xff0c;百年树人&#xff0c;教育在人类进步中的作用不言而喻&#xff0c;教育的每一次变革也牵动着。随着科技的进步&#xff0c;教育行业的工具、运营模式等也面临着变革。不过&#xff0c;笔者认为&#xff0c;作为百年大计的一项事业&#xff0c;追求一夜之间…

ocilib操作BLOB类型的字段

1、理解long、long raw、clob、blob的区别: (1)、long用来存储可边长度“字符串”,最大长度是2GB,对于超出一定长度的文本,只能用long类型来存储,并且一个表只能包含一个long类型; (2)、long raw用于存储二进值数据,最大2GB,并且一个表只能包含一个long raw类型;…

ocilib操作Date字段类型

建表,模拟测试时使用: create table TEST_DIRECTPATH (VAL_INT NUMBER(8,4),VAL_STR VARCHAR2(30),VAL_DATE DATE ) 1、插入日期型数据: #include "stdafx.h" #include <iostream> #include <fstream> #include <sstream> #include "…

大数据之心不死,IBM 推出首个一站式 AI 分析服务

IBM 在大数据领域的野心昭然若揭。据国外媒体报道&#xff0c;近日&#xff0c;该公司推出了一项基于人工智能大数据平台的一站式分析服务&#xff0c;Project DataWorks 。 IBM 表示&#xff0c;这将是第一个整合所有类型数据&#xff0c;并利用 AI 进行分析的大数据平台。目…

ocilib批量插入数据

1、建表: create table TEST_ARRAY (VAL_INT NUMBER,VAL_DBL FLOAT,VAL_FLT FLOAT,VAL_STR VARCHAR2(30),VAL_DATE DATE ) 2、批量插入操作: #include "stdafx.h" #include <iostream> #include <fstream> #include <sstream> #include &…

数据结构与算法之LeetCode-828. 统计子串中的唯一字符 - 力扣

828. 统计子串中的唯一字符 - 力扣&#xff08;LeetCode&#xff09; var uniqueLetterString function(s) {const index new Map();for (let i 0; i < s.length; i) {const c s[i];if (!index.has(c)) {index.set(c, []);index.get(c).push(-1);}index.get(c).push(i)…

光纤跳线在高密度数据中心的应用

光纤跳线在数据中心的应用非常广泛&#xff0c;而且近些年来数据中心光纤传输系统对带宽的需求显示出高增长的趋势&#xff0c;从而使用新一代的光纤和光模块可以继续探索光纤网络带宽增长的潜力。由于多模光纤跳线在成本方面具有很大的优势&#xff0c;促使其在数据中心的应用…

ocilib调用存储过程

1、输出参数为一个查询游标: (1)创建存储过程: CREATE OR REPLACE PROCEDURE GETCURSOR(MYCURSOR OUT SYS_REFCURSOR) AS BEGIN OPEN MYCURSOR FOR SELECT * FROM dept; END; (2)编写程序调用存储过程: #include "stdafx.h" #include <iostream> #i…