mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
0222完全二叉树的节点个数JavaScript版本
This commit is contained in:
@ -243,6 +243,74 @@ Python:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
JavaScript:
|
||||||
|
|
||||||
|
递归版本
|
||||||
|
```javascript
|
||||||
|
var countNodes = function(root) {
|
||||||
|
//递归法计算二叉树节点数
|
||||||
|
// 1. 确定递归函数参数
|
||||||
|
const getNodeSum=function(node){
|
||||||
|
//2. 确定终止条件
|
||||||
|
if(node===null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
//3. 确定单层递归逻辑
|
||||||
|
let leftNum=getNodeSum(node.left);
|
||||||
|
let rightNum=getNodeSum(node.right);
|
||||||
|
return leftNum+rightNum+1;
|
||||||
|
}
|
||||||
|
return getNodeSum(root);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代(层序遍历)版本
|
||||||
|
```javascript
|
||||||
|
var countNodes = function(root) {
|
||||||
|
//层序遍历
|
||||||
|
let queue=[];
|
||||||
|
if(root===null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
queue.push(root);
|
||||||
|
let nodeNums=0;
|
||||||
|
while(queue.length){
|
||||||
|
let length=queue.length;
|
||||||
|
while(length--){
|
||||||
|
let node=queue.shift();
|
||||||
|
nodeNums++;
|
||||||
|
node.left&&queue.push(node.left);
|
||||||
|
node.right&&queue.push(node.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nodeNums;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
利用完全二叉树性质
|
||||||
|
```javascript
|
||||||
|
var countNodes = function(root) {
|
||||||
|
//利用完全二叉树的特点
|
||||||
|
if(root===null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
let left=root.left;
|
||||||
|
let right=root.right;
|
||||||
|
let leftHeight=0,rightHeight=0;
|
||||||
|
while(left){
|
||||||
|
left=left.left;
|
||||||
|
leftHeight++;
|
||||||
|
}
|
||||||
|
while(right){
|
||||||
|
right=right.right;
|
||||||
|
rightHeight++;
|
||||||
|
}
|
||||||
|
if(leftHeight==rightHeight){
|
||||||
|
return Math.pow(2,leftHeight+1)-1;
|
||||||
|
}
|
||||||
|
return countNodes(root.left)+countNodes(root.right)+1;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user