mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-28 19:43:31 +08:00
c++
This commit is contained in:
parent
1a07e30fb1
commit
5101285c73
@ -1497,8 +1497,36 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
补充:判断一颗树是不是完全二叉树:
|
||||||
|
|
||||||
|
LeetCode 958. 二叉树的完全性检验
|
||||||
|
|
||||||
|
可以根据编号找规律:
|
||||||
|
|
||||||
|
如果是完全二叉树的话:
|
||||||
|
|
||||||
|
- (节点的总个数) 一定 是 等于 (最大的节点编号 )
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
class Solution{
|
||||||
|
public:
|
||||||
|
int n = 0 , p = 0;
|
||||||
|
bool dfs(TreeNode * root, int k)
|
||||||
|
{
|
||||||
|
if(!root)return true;
|
||||||
|
if(k>100)return false;//溢出
|
||||||
|
n++ , p = max(p,k);
|
||||||
|
return dfs(root->left,2*k) && dfs(root->right,2*k+1);
|
||||||
|
}
|
||||||
|
bool isCompleteTree(TreeNode * root)
|
||||||
|
{
|
||||||
|
if(!dfs(root,1))return false;//1表示传进去的是根节点,return false表示节点个数溢出
|
||||||
|
return n == p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
BIN
exampleSite/content/post/给个offer/图片/1714230817991.png
Normal file
BIN
exampleSite/content/post/给个offer/图片/1714230817991.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 396 KiB |
Loading…
Reference in New Issue
Block a user