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 8d93b1e

Browse files
Merge pull request #334 from X-shuffle/master
添加 0222.完全二叉树的节点个数 go版本
2 parents 0a2a5b1 + 39f852e commit 8d93b1e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,35 @@ class Solution:
308308

309309
Go:
310310

311+
递归版本
312+
313+
```go
314+
/**
315+
* Definition for a binary tree node.
316+
* type TreeNode struct {
317+
* Val int
318+
* Left *TreeNode
319+
* Right *TreeNode
320+
* }
321+
*/
322+
//本题直接就是求有多少个节点,无脑存进数组算长度就行了。
323+
func countNodes(root *TreeNode) int {
324+
if root == nil {
325+
return 0
326+
}
327+
res := 1
328+
if root.Right != nil {
329+
res += countNodes(root.Right)
330+
}
331+
if root.Left != nil {
332+
res += countNodes(root.Left)
333+
}
334+
return res
335+
}
336+
```
337+
338+
339+
311340
JavaScript:
312341

313342
递归版本

0 commit comments

Comments
(0)

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