forked from nch3ng/leetcode-1
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbalanced_binary_tree.rb
More file actions
30 lines (25 loc) · 811 Bytes
/
Copy pathbalanced_binary_tree.rb
File metadata and controls
30 lines (25 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# https://leetcode.com/problems/balanced-binary-tree/
#
# Given a binary tree, determine if it is height-balanced. For this problem,
# a height-balanced binary tree is defined as a binary tree in which the
# depth of the two subtrees of every node never differ by more than 1.
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {Boolean}
def is_balanced(root)
_is_balanced_(root) != -1
end
private def _is_balanced_(root)
return 0 if root.nil?
return -1 if (ldepth = _is_balanced_(root.left )) == -1
return -1 if (rdepth = _is_balanced_(root.right)) == -1
return -1 if (ldepth - rdepth).abs > 1
return [ldepth, rdepth].max + 1
end