zoukankan      html  css  js  c++  java
  • 655.Print Binary Tree 打印二叉树

    Print a binary tree in an m*n 2D string array following these rules:

    1. The row number m should be equal to the height of the given binary tree.
    2. The column number n should always be an odd number.
    3. The root node's value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don't need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don't need to leave space for both of them.
    4. Each unused space should contain an empty string "".
    5. Print the subtrees following the same rules.

    Example 1:

    Input:
         1
        /
       2
    Output:
    [["", "1", ""],
     ["2", "", ""]]
    

    Example 2:

    Input:
         1
        / 
       2   3
        
         4
    Output:
    [["", "", "", "1", "", "", ""],
     ["", "2", "", "", "", "3", ""],
     ["", "", "4", "", "", "", ""]]
    

    Example 3:

    Input:
          1
         / 
        2   5
       / 
      3 
     / 
    4 
    Output:
    
    [["",  "",  "", "",  "", "", "", "1", "",  "",  "",  "",  "", "", ""]
     ["",  "",  "", "2", "", "", "", "",  "",  "",  "",  "5", "", "", ""]
     ["",  "3", "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]
     ["4", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]
    

    Note: The height of binary tree is in the range of [1, 10].

    将二叉树输出成m * n二维数组

    行数m等于二叉树的高度

    列数n总是奇数

    根节点位于首行正中间,将其下的空间分成左右两半。递归此过程。

    1. let Tree = require('../Helper/Javascript/Tree').Tree;
    2. /**
    3. * Definition for a binary tree node.
    4. * function TreeNode(val) {
    5. * this.val = val;
    6. * this.left = this.right = null;
    7. * }
    8. */
    9. /**
    10. * @param {TreeNode} root
    11. * @return {string[][]}
    12. */
    13. var printTree = function (root) {
    14. let rowNum = maxDepth(root);
    15. let colNum = Math.pow(2, rowNum - 1) * 2 - 1;
    16. let res = [];
    17. for (let i = 0; i < rowNum; i++) {
    18. res[i] = [];
    19. for (let j = 0; j < colNum; j++) {
    20. res[i][j] = "";
    21. }
    22. }
    23. setNodeNum(root, res, 0, 0, colNum);
    24. return res;
    25. };
    26. var setNodeNum = function (node, res, depth, left, right) {
    27. if (!node) return;
    28. let mid = Math.floor((left + right) / 2);
    29. res[depth][mid] = String(node.val);
    30. setNodeNum(node.left, res, depth + 1, left, mid);
    31. setNodeNum(node.right, res, depth + 1, mid, right);
    32. }
    33. var maxDepth = function (root) {
    34. if (!root) return 0;
    35. let left = maxDepth(root.left);
    36. let right = maxDepth(root.right);
    37. return Math.max(left, right) + 1;
    38. }
    39. // let root = Tree.CreateTree([1, 2, 5, 3, null, null, null, 4, 5]);
    40. // console.log(printTree(root));







  • 相关阅读:
    ligerui_ligerTree_007_ligerTree动态加载节点
    ligerui_ligerTree_006_ligerui事件支持
    ligerui_ligerTree_005_动态增加“树”节点
    ligerui_ligerTree_004_对"ligerTree"节点操作
    ligerui_ligerTree_003_配置url参数,加载“树”
    ligerui_ligerTree_002_利用JavaScript代码配置ligerTree节点
    ligerui_ligerTree_001_第一个“树”效果
    ligerui_实际项目_003:form中添加数据,表格(grid)里面显示,最后将表格(grid)里的数据提交到servlet
    ligerui+json_002_Grid用法、属性总结
    ligerui+json_001_实现表格(grid)的后台数据显示、分页
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7795892.html
Copyright © 2011-2022 走看看