From 39f852e656a10cc5558148fb31c4f93f4c455d6f Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 6 Jun 2021 22:13:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0222.完全二叉树的节点个数 go版本 --- .../0222.完全二叉树的节点个数.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 91e24247..998e22f3 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -308,6 +308,35 @@ class Solution: Go: +递归版本 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +//本题直接就是求有多少个节点,无脑存进数组算长度就行了。 +func countNodes(root *TreeNode) int { + if root == nil { + return 0 + } + res := 1 + if root.Right != nil { + res += countNodes(root.Right) + } + if root.Left != nil { + res += countNodes(root.Left) + } + return res +} +``` + + + JavaScript: 递归版本