Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - //https://leetcode.com/problems/balanced-binary-tree/
 - class Solution {
 - private var minH = Int.MAX_VALUE
 - private var maxH = 0
 - fun isBalanced(root: TreeNode?): Boolean {
 - dfsHight(root, 1)
 - return Math.abs(minH -maxH) <= 1
 - }
 - private fun dfsHight(node: TreeNode?, h: Int) {
 - println("val=${node?.`val`}, h=$h")
 - if(node == null) {
 - if(h < minH) minH = h
 - if(h > maxH) maxH = h
 - println("minH=$minH, max=$maxH")
 - return
 - }
 - dfsHight(node.left, h + 1)
 - dfsHight(node.right, h + 1)
 - }
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment