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. 四十八、在SAP中函数参数的使用

    一.不带参数的函数定义如下 二.我们把函数内部会变化的变量以参数的形式定义,其中USING和CHANGING分别表示变量和返回值,因为so_car表示的是工作区,所以后面加上[]符号. 三.我们把2个 ...

  2. 163-PHP 文本替换函数str_replace(四)

    <?php $str='Hello world!'; //定义源字符串 $search=array('o','l','w'); //定义将被替换的字符数组 $replace=array('1', ...

  3. 109-PHP类成员初始化值

    <?php class mao{ //定义猫类 public $age=0; //定义多个属性并初始化 public $weight=50; public $color='white'; } $ ...

  4. 实验吧-杂项-你知道他是谁吗?(转盘密码、NTFS数据流检测及导出)

    刚看到的时候听懵,没注意到重点,其实很多时候题目中的细节就是给我们线索的,所以审题和思考是很重要的. 在没做到点上的是,也做了一点努力,没有效果,科普一下这个人((*^▽^*))图片上是托马斯.杰斐逊 ...

  5. mysql 事务使用教程

    一.什么是事务 事务Transaction,是指作为一个基本工作单元执行的一系列SQL语句的操作,要么完全地执行,要么完全地都不执行.   二.事务的特性       原子性         事务是一 ...

  6. caffe 官方demo python api

    Jupyter https://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb 涉及: - ...

  7. log4j的配置学习

    我的 log4j.properties: # Set root category priority to INFO and its only appender to CONSOLE. #log4j.r ...

  8. 6 ~ express ~ 搭建用户注册前端页面

    一,前端页面 /views/login.html <!DOCTYPE html> <html lang="en"> <head> <met ...

  9. Linux基础(1) 安装

    Linux基础 一.创建CentOS 7 Linux虚拟机 1.安装虚拟机     桥接网络:相当于这台机器就是物理机,多个电脑在连接在一个交换机上,同一个子网 NAT:这台机器只能通过物理机(相当于 ...

  10. spring boot 生命周期初探

    1.MytestApplication package com.gomepay; import org.springframework.boot.Banner; import org.springfr ...