Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0843f22

Browse files
committed
Update 222.完全二叉树的节点个数,添加C#递归版
1 parent cfc78c3 commit 0843f22

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0222.完全二叉树的节点个数.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,31 @@ impl Solution {
867867
}
868868
}
869869
```
870+
### C#
871+
```C#
872+
// 递归
873+
public int CountNodes(TreeNode root)
874+
{
875+
if (root == null) return 0;
876+
var left = root.left;
877+
var right = root.right;
878+
int leftDepth = 0, rightDepth = 0;
879+
while (left != null)
880+
{
881+
left = left.left;
882+
leftDepth++;
883+
}
884+
while (right != null)
885+
{
886+
right = right.right;
887+
rightDepth++;
888+
}
889+
if (leftDepth == rightDepth)
890+
return (int)Math.Pow(2, leftDepth+1) - 1;
891+
return CountNodes(root.left) + CountNodes(root.right) + 1;
892+
893+
}
894+
```
870895

871896
<p align="center">
872897
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /