Skip to content

Commit

Permalink
222. 完全二叉树的节点个数
Browse files Browse the repository at this point in the history
  • Loading branch information
BGMer7 committed Jul 29, 2022
1 parent bf9ece3 commit 2a81fb0
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/com/gatsby/_222CountCompleteTreeNodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.gatsby;

import java.util.Objects;

/**
* @ClassName: _222CountCompleteTreeNodes
* @Description: leetcode 222 完全二叉树的节点数量
* @author: Gatsby
* @date: 2022/7/29 10:48
*/

public class _222CountCompleteTreeNodes {
/**
* @MethodName: countNodes
* @Parameter: [root]
* @Return int
* @Description: 递归
* @author: Gatsby
* @date: 2022/7/29 10:50
*/
public int countNodes(TreeNode root) {
if (Objects.isNull(root)) {
return 0;
} else return countNodes(root.left) + countNodes(root.right) + 1;
}

/**
* @MethodName: countNodesBs
* @Parameter: [root]
* @Return int
* @Description: 二分查找
* @author: Gatsby
* @date: 2022/7/29 10:58
*/
public int countNodesBs(TreeNode root) {
if (Objects.isNull(root)) return 0;
int left = countHeight(root.left);
int right = countHeight(root.right);

// 如果左子树和右子树的深度相等,那么左子树一定是个满二叉树,加上一个root节点
// 直接用1 << left计算就行
if (left == right) {
return countNodesBs(root.right) + (1 << left);
}
return countNodesBs(root.left) + (1 << right);
}

private int countHeight(TreeNode root) {
int height = 0;
while (!Objects.isNull(root)) {
height++;
root = root.left;
}
return height;
}
}


0 comments on commit 2a81fb0

Please sign in to comment.