-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path222-countCompleteTree.java
More file actions
36 lines (34 loc) · 1019 Bytes
/
222-countCompleteTree.java
File metadata and controls
36 lines (34 loc) · 1019 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution_222 {
public int countNodes(TreeNode root) {
// 简单的分治
if(root == null) return 0;
return 1+countNodes(root.left)+countNodes(root.right);
}
// 根据左右子树的高度情况,利用了完全二叉树的性质
public int countNodes_2(TreeNode root) {
if(root == null) return 0;
int left = depth(root.left);
int right = depth(root.right);
// 左子树不一定满,右子树满
if(left == right + 1) return countNodes(root.left) + (1<<right);
else return countNodes(root.right) + (1<<left);
}
// 直接访问左子树得到深度
public int depth(TreeNode root) {
int count = 0;
while(root != null) {
count ++;
root = root.left;
}
return count;
}
}