sanya5791

110. Balanced Binary Tree

Jul 29th, 2021
953
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data