-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
56 lines (51 loc) · 1.42 KB
/
Solution.java
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
package Tree.medium.No_222_Count_Complete_Tree_Nodes;
import Tree.TreeNode;
import java.util.Stack;
/**
* FileName: Solution
* Author: EdisonLi的Windows
* Date: 2019/5/6 17:22
* Description: 222. Count Complete Tree Nodes
* 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
* Difficulty: Medium
*/
public class Solution {
int total = 0;
public int countNodes(TreeNode root) {
if (root == null) return 0;
handleNodes(root);
return total;
}
private void handleNodes(TreeNode root) {
if (root == null) return;
total++;
handleNodes(root.left);
handleNodes(root.right);
}
public int countNodes1(TreeNode root){
if (root == null) return 0;
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
while (!stack.isEmpty()){
TreeNode peek = stack.pop();
total++;
if (peek.left != null) stack.push(peek.left);
if (peek.right != null) stack.push(peek.right);
}
return total;
}
}