Given a binary tree, count the number of nodes in each node’s left subtree, and store it in the numNodesLeft field.

Examples

1(6)

/          \

2(3)        3(0)

/      \

4(1)     5(0)

/        \        \

6(0)     7(0)   8(0)

The numNodesLeft is shown in parentheses.

/**
* public class TreeNodeLeft {
* public int key;
* public TreeNodeLeft left;
* public TreeNodeLeft right;
* public int numNodesLeft;
* public TreeNodeLeft(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public void numNodesLeft(TreeNodeLeft root) {
// Write your solution here
helper(root);
} private int helper(TreeNodeLeft root) {
if (root == null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
root.numNodesLeft = left;
return 1 + left + right;
}
}

[Algo] 646. Store Number Of Nodes In Left Subtree的更多相关文章

  1. This means that only a small number of nodes must be read from disk to retrieve an item.

    http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html Introduction A B-tree is a specialized ...

  2. Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

    我用String代替了链表显示,本题的大意是每k个进行逆序处理,剩下的不够k个的就按照原顺序保留下来. public class ReverseNodes { public static void m ...

  3. Kth Smallest Element in a BST 解答

    Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...

  4. 【433】COMP9024 复习

    目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...

  5. Print Common Nodes in Two Binary Search Trees

    Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...

  6. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

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

  7. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. No.025:Reverse Nodes in k-Group

    问题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  9. Reverse Nodes in k-Group

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

随机推荐

  1. 如何让手游内存占用更小?从内存消耗iOS实时统计开始

    为什么iOS内存使用过多会崩溃,性能会下降?腾讯游戏学院专家Devlin在本文给了解释,如何让手游内存占用更小?从内存消耗iOS实时统计开始. 一.问题 在之前的手游项目中,内存使用过多,都开始崩溃了 ...

  2. flink笔记(三) flink架构及运行方式

    架构图 Job Managers, Task Managers, Clients JobManager(Master) 用于协调分布式执行.它们用来调度task,协调检查点,协调失败时恢复等. Fli ...

  3. Day 16:输入输出字符流、缓冲输入字符流

    输入输出字节流输出字符时的常见问题 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...

  4. 2019山东ACM省赛L题题解(FLOYD传递闭包的变形)

    本题地址 https://cn.vjudge.net/contest/302014#problem/L Median Time Limit: 1 Second      Memory Limit: 6 ...

  5. Mybatis(1)-初识mybaits

    一.概述 1.概述 mybatis 是一个优秀的基于 java 的持久层框架,它内部封装了 jdbc,使开发者只需要关注 sql 语句本身, 而不需要花费精力去处理加载驱动.创建连接.创建 state ...

  6. 二十七、CI框架之自己写分页类并加载(写分页还是有难度,搞了一整天)

    一.我们写好自己的分页代码,防止library目录中,带构造函数 二.在模型中,添加2个函数,一个是查询数据的条数,第二个是取出数据库中的数据 三.在控制中,写入相应的代码,如下: 四.在界面中,写入 ...

  7. centos7 lvm合并分区脚本初探-linux性能测试 -centos7修改网卡名字-jdk环境安装脚本-关键字查询文件-批量添加用户

    1.#!/bin/bash lvmdiskscan | grep centos > /root/a.txt a=`sed -n '1p' /root/a.txt` b=`sed -n '2p' ...

  8. div自动适应高度

    div高度100%<!DOCTYPE html> <html> <head></head> <body> <div id=" ...

  9. loback.xml 在idea中代码自动完成

    1.下载xsd文件 2.idea添加xsd文件 URI: http://ch.qos.logback/xml/ns/logback File: D:\env\plugins\logback\logba ...

  10. vue中的axios请求

    1.get请求 // get this.$axios.get('请求路径') .then(function (response) { console.log(response); // 成功 }) . ...