-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCompleteTree.java
More file actions
40 lines (37 loc) · 977 Bytes
/
Copy pathCompleteTree.java
File metadata and controls
40 lines (37 loc) · 977 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
31
32
33
34
35
36
37
38
39
40
package com.algorithdemo.tree;
import java.util.LinkedList;
import java.util.Queue;
/**
* 判断二叉树是否为完全二叉树
*
* Created by geguofeng on 2018/1/15.
*/
public class CompleteTree {
public static boolean isCRTree(TreeNode head){
if(head == null){
return true;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
boolean leaf = false;
TreeNode l = null;
TreeNode r = null;
queue.offer(head);
while (!queue.isEmpty()){
head = queue.poll();
l = head.getLeft();
r = head.getRight();
if ((leaf && (l !=null || r!=null)) ||(l == null && r!= null)){
return false;
}
if(l != null){
queue.offer(l);
}
if(r != null){
queue.offer(r);
}else {
leaf = true;
}
}
return true;
}
}