forked from acearth/LeetCodePractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completeTreeNodes.rb
62 lines (57 loc) · 993 Bytes
/
completeTreeNodes.rb
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require './base/TreeNode'
def heightOfCompleteTree(root)
return 0 if root==nil
return 1+ heightOfCompleteTree(root.left)
end
def count_nodes(root)
height=heightOfCompleteTree(root)
return 0 if height==0
h=2**(height-1)
l=0
while h-l>1
m=(h+l)/2
if checkNode(root, height, m)==true
l=m+1
else
h=m
end
l-=1 if l==h
end
l-=1 if checkNode(root,height,l)==false
return 2**(height-1)+l
end
def checkNode(root, height, seq)
path=seq.to_s(2)
d=height-1-path.length
if d>0
pre=String.new
d.times do
pre+="0"
end
path=pre+path
end
p=root
(height-1).times do |i|
if path[i]=="0"
p=p.left
else
p=p.right
end
return false if p==nil&&i<height-1
end
return true
end
t1=TreeNode.new(1)
t2=TreeNode.new(2)
t3=TreeNode.new(3)
t4=TreeNode.new(4)
t5=TreeNode.new(5)
t6=TreeNode.new(6)
t7=TreeNode.new(7)
t1.left=t2
t1.right=t3
t2.left=t4
t2.right=t5
t3.left=t6
t3.right=t7
p count_nodes t1