Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2534 from VoxDai/patch-2
Browse files Browse the repository at this point in the history
Update 0222.完全二叉树的节点个数.md / Fix Typo
  • Loading branch information
youngyangyang04 authored May 15, 2024
2 parents 56a7c68 + 19b7672 commit 14ff932
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions problems/0222.完全二叉树的节点个数.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@
1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。

代码如下:
```
```CPP
int getNodesNum(TreeNode* cur) {
```
2. 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
代码如下:
```
```CPP
if (cur == NULL) return 0;
```

3. 确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。

代码如下:

```
```CPP
int leftNum = getNodesNum(cur->left); //
int rightNum = getNodesNum(cur->right); //
int treeNum = leftNum + rightNum + 1; //
Expand Down

0 comments on commit 14ff932

Please sign in to comment.