Advertisement
sanya5791

110. Balanced Binary Tree

Jul 29th, 2021
1,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.65 KB | None | 0 0
  1. //https://leetcode.com/problems/balanced-binary-tree/
  2. class Solution {
  3.    
  4.     private var minH = Int.MAX_VALUE
  5.     private var maxH = 0
  6.    
  7.     fun isBalanced(root: TreeNode?): Boolean {
  8.         dfsHight(root, 1)
  9.         return Math.abs(minH -maxH) <= 1
  10.     }
  11.    
  12.     private fun dfsHight(node: TreeNode?, h: Int) {
  13.         println("val=${node?.`val`}, h=$h")
  14.         if(node == null) {
  15.             if(h < minH) minH = h
  16.             if(h > maxH) maxH = h
  17.             println("minH=$minH, max=$maxH")
  18.            
  19.             return
  20.         }
  21.        
  22.  
  23.         dfsHight(node.left, h + 1)
  24.         dfsHight(node.right, h + 1)
  25.     }
  26.    
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement