zoukankan      html  css  js  c++  java
  • 655. Print Binary Tree

    package LeetCode_655
    
    /**
     * 655. Print Binary Tree
     * https://leetcode.com/problems/print-binary-tree/description/
     * Example 1:
    Input:
    1
    /
    2
    Output:
    [["", "1", ""],
    ["2", "", ""]]
     * */
    
    class TreeNode(var `val`: Int) {
        var left: TreeNode? = null
        var right: TreeNode? = null
    }
    
    class Solution {
        fun printTree(root: TreeNode?): List<List<String>> {
            //get height and width
            val h = getHeight(root)
            val w = (1 shl h) - 1//2^h-1
            //print by h and w
            val result = ArrayList<ArrayList<String>>()
            //init
            for (i in 0 until h) {
                val list = ArrayList<String>()
                for (j in 0 until w) {
                    list.add("")
                }
                result.add(list)
            }
            fill(root, result, 0, 0, w - 1)
            return result
        }
    
        private fun fill(node: TreeNode?, result: ArrayList<ArrayList<String>>, height: Int, l: Int, r: Int) {
            if (node == null) {
                return
            }
            val mid = (l + r) / 2
            result[height][mid] = node.`val`.toString()
            fill(node.left, result, height + 1, l, mid - 1)
            fill(node.right, result, height + 1, mid + 1, r)
        }
    
        private fun getHeight(node: TreeNode?): Int {
            if (node == null) {
                return 0
            }
            return Math.max(getHeight(node.left), getHeight(node.right)) + 1
        }
    }
  • 相关阅读:
    事后诸葛亮
    OVS常用命令
    阿里云部署杂记
    Alpha冲刺总结
    测试随笔
    Alpha冲刺集合
    项目Alpha冲刺Day12
    项目Alpha冲刺Day11
    项目Alpha冲刺Day10
    MySQL修改密码
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12856658.html
Copyright © 2011-2022 走看看