Prompt

Node Depths
The distance between a node in a Binary Tree and the tree’s root is called the node’s depth. Write a function that takes in a Binary Tree and returns the sum of its nodes’ depths.

Each BinaryTree node has an integer value , a left child node, and a right child node. Children nodes can either be BinaryTree nodes themselves or None / null .

Sample Input

Sample Output

16

Solution 1

import java.util.*;class Program {// Average case: when the tree is balanced// O(n) time | O(h) space - where n is the number of nodes in// the Binary Tree and h is the height of the Binary Tree.public static int nodeDepths(BinaryTree root) {int sumOfDepth = 0;//AList<Level> stack = new ArrayList<Level>();//Bstack.add(new Level(root,0));while (stack.size() > 0) {//CLevel top = stack.remove(stack.size() - 1);BinaryTree node = top.root;int depth = top.depth;if (node == null) continue;sumOfDepth += depth;stack.add(new Level(node.left, depth + 1));stack.add(new Level(node.right, depth + 1));}return sumOfDepth;}static class Level {public BinaryTree root;int depth;public Level(BinaryTree root, int depth) {this.root = root;this.depth = depth;}}static class BinaryTree {int value;BinaryTree left;BinaryTree right;public BinaryTree(int value) {this.value = value;left = null;right = null;}}
}

# 1A

一定要初始化

# 1B

用简单的数组+stack的思想

# 1C

length、size、size()

Solution 2

import java.util.*;class Program {// Average case: when the tree is balanced// O(n) time | O(h) space - where n is the number of nodes in// the Binary Tree and h is the height of the Binary Tree.public static int nodeDepths(BinaryTree root) {return nodeDepthsHelper(root, 0);}public static int nodeDepthsHelper(BinaryTree root, int depth) {if (root == null) return 0;return depth + nodeDepthsHelper(root.left, depth + 1) + nodeDepthsHelper(root.right, depth + 1);}static class BinaryTree {int value;BinaryTree left;BinaryTree right;public BinaryTree(int value) {this.value = value;left = null;right = null;}}
}

Node Depths相关推荐

  1. LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素

    前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...

  2. Node.js 简单入门

    目录 一. 什么是Nodejs 二. Nodejs组成图(对比jdk) 三. Nodejs的安装 四. 第一个Nodejs程序 五. Node实现请求响应 六. Node操作MYSQL数据库 1. 安 ...

  3. CentOS7的node.js安装

    一.方式1:yum安装 这里我们指定安装8.x以上的版本 curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo ba ...

  4. Node搭建多人聊天室

    博客初期准备使用的通讯,后被荒废. 使用的是nodejs-websocket,未使用socket.io 用来玩玩的东西,了解下通讯协议. 演示地址:socket.amayaliu.cn git地址:h ...

  5. node.js(四)小结

    用之前的内容写一个简单的注册登录 const http = require('http') const urlLib = require('url') const fs = require('fs') ...

  6. node.js(node.js+mongoose小案例)_实现简单的注册登录退出

    一.前言 通过node.js基本知识对node.js基本知识的一个简单应用 1.注册 2.登录 3.退出 二.基本内容 1.项目结构搭建如图所示 2.这个小案列中用到了art-template子模板以 ...

  7. node、Mongo项目如何前后端分离提供接口给前端

    node接口编写,vue-cli代理接口方法  通常前端使用的MocK 数据的方法,去模拟假的数据,但是如果有node Mongodb 去写数据的话就不需要在去mock 数据了,具体的方法如下. 首先 ...

  8. linux安装语言总是waiting,node的serialport在arm的Linux安装血泪史之我太难了

    0.序 本人在arm的Linux系统上serialport耗时5天,现在想想都头皮发麻! 下面我简单说一下过程,由于error时没有截图留档,关于出现的error,我就用语言描述一下,要是有什么不理解 ...

  9. Virtual host / experienced an error on node rabbit@wohu-rabbit and may be inaccessible

    RabbitMQ 如下错误: Virtual host / experienced an error on node rabbit@wohu-rabbit and may be inaccessibl ...

最新文章

  1. 多传感器融合 | R-LINS概述
  2. jittor和pytorch网络对比之context_encoder
  3. golang 开发 Struct 转换成 map 两种方式比较
  4. 程序员面试金典 - 面试题 16.06. 最小差(排序+双指针)
  5. Python json.dumps() 自动缩进
  6. 关于List转Json的简单方法
  7. 基于51单片机用按键和nrf24l01模块控制小车移动
  8. 怎么查看python下opencv版本
  9. Cityscapes数据集gtFine部分介绍
  10. excel的poi和EasyExcel的基本读写
  11. 电脑安装完软件打不开怎么办
  12. 全链路监控Jaeger
  13. C语言基础课 编写程序之1.百元买鸡,公鸡一只5元,母鸡一只3元,小鸡3只一元,现将一百元要买一百只鸡,公鸡母鸡小鸡各多少只2.编程求1~200中能被2除余1或能被3除余1或能被5除余1的前10个整数
  14. PDF转Word方法大盘点:看了这一篇,就不用再找转换技巧了
  15. 巴西龟购买指南(避免掉坑)
  16. Redis系列(六)
  17. unity GetComponent使用
  18. 典当行最多能够抵押多长时间
  19. 【2021-09-22 修订】【梳理】计算机网络:自顶向下方法 附录一 物理层与通信基础
  20. 2016012028 赵莉 散列函数的应用及其安全性

热门文章

  1. 精益生产之MES制造执行系统
  2. Ionic4使用crosswalk-webview内核
  3. urdf和rviz的初步使用
  4. 我是怎么把“酒足饭饱”的微信公众号做到5000+人关注的。
  5. Influence Maximization影响力最大化
  6. Flash For Server[FFS]
  7. 财务大集中虚拟化解决方案
  8. 友达13.3寸高分辨率液晶屏G133ZAN01.1-13.3寸工控屏
  9. C#利用方法重载实现2个整数,2个双精度数,3个整数中求最大值。由于设计了方法重载,系统会根据调用方法时传递的实参类型和个数而自动选择相应的方法来求最大数。
  10. 转载的一篇文章,如何上好第一堂课