-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0222_count_complete_tree_nodes.java
More file actions
63 lines (48 loc) · 1.61 KB
/
0222_count_complete_tree_nodes.java
File metadata and controls
63 lines (48 loc) · 1.61 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
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* https://leetcode-cn.com/problems/count-complete-tree-nodes/submissions/
给出一个完全二叉树,求出该树的节点个数。
说明:完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
示例:
输入:
1
/ \
2 3
/ \ /
4 5 6
输出: 6
------------------------------------------------------------------------------------------
Given a complete binary tree, count the number of nodes.
Note: Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Example:
Input:
1
/ \
2 3
/ \ /
4 5 6
Output: 6
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class MySolution {
public int countNodes(TreeNode root) {
if (root == null)
return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
}
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
if (root.left == null) return 1;
if (root.right == null) return 2;
return countNodes(root.left) + countNodes(root.right) + 1;
}
}