博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
222. Count Complete Tree Nodes
阅读量:7109 次
发布时间:2019-06-28

本文共 976 字,大约阅读时间需要 3 分钟。

Given a complete binary tree, count the number of nodes.

Note:

Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

Example:

Input:     1   / \  2   3 / \  /4  5 6Output: 6

难度:medium

题目:给定完全二叉树,统计其结点数。

思路:后序遍历

Runtime: 1 ms, faster than 100.00% of Java online submissions for Count Complete Tree Nodes.

Memory Usage: 40.4 MB, less than 45.43% of Java online submissions for Count Complete Tree Nodes.

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */class Solution {    public int countNodes(TreeNode root) {        if (null == root) {            return 0;        }                return countNodes(root.left) + countNodes(root.right) + 1;    }}

转载地址:http://javhl.baihongyu.com/

你可能感兴趣的文章
实用工具箱app开发日记3
查看>>
深入理解计算机系统9——虚拟存储器
查看>>
新しい道に、頑張ります!
查看>>
删除有序链表中重复的项
查看>>
IQD文件模板以及cs7g.ini信息
查看>>
(C#版本)提升SQlite数据库效率——开启事务,极速插入数据,3秒100万,32秒1000万条数据...
查看>>
10.1(java学习笔记)JDBC基本操作(连接,执行SQL语句,获取结果集)
查看>>
java多线程(7)实现一个线程池
查看>>
任务01——谈谈对参加工作室的预期
查看>>
VUE 动态切换列表active样式
查看>>
【转载】线程同步
查看>>
android 流行框架的使用
查看>>
Windows Phone 7中用好Silverlig“.NET研究”ht开发利器
查看>>
互联网周刊:互联网进化论,互联网营销
查看>>
ios11 bug总结
查看>>
cakephp上传组件教程
查看>>
flume【源码分析】分析Flume的启动过程
查看>>
经典算法 Manacher算法详解
查看>>
关于System.out.println()与System.out.print("\n")的区别
查看>>
HRMS(人力资源管理系统)-从单机应用到SaaS应用-架构分析(功能性、非功能性、关键约束)-下篇...
查看>>