-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL222.java
More file actions
50 lines (45 loc) · 1.46 KB
/
L222.java
File metadata and controls
50 lines (45 loc) · 1.46 KB
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
package com.liang.leetcode.binarySearch;
/**
* @ClassName: L222
* @Description: 完全二叉树的节点个数
* @Author: LiaNg
* @Date: 2020/3/24 19:55
*/
public class L222 {
/**
* 给出一个完全二叉树,求出该树的节点个数。
* 说明:
* 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/count-complete-tree-nodes
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int left = countLevel(root.left);
int right = countLevel(root.right);
if (left == right) {
return countNodes(root.right) + (1 << left);
} else {
return countNodes(root.left) + (1 << right);
}
}
private int countLevel(TreeNode root) {
int level = 0;
while (root != null) {
level++;
root = root.left;
}
return level;
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}