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. AS-PATH(路径属性)路由路径欺骗术

    AS-PATH(路径属性)路由路径欺骗术: ①:抓取感兴趣流量——前缀与访问 ②:创建路由地图 ③:路由地图第一法则——permit 10 ④:在第一法则中,匹配(感兴趣流量) ⑤:设置 路径欺骗术— ...

  2. 四、JavaScript之<script>标签的使用

    一.代码如下 二.运行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...

  3. 132-PHP子类和父类同名函数的调用

    <?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...

  4. Pyinstaller的安装及简单使用

    (1)安装: 用传统的pip install pyinstaller出错,在https://pypi.org/project/PyInstaller/#files上下载PyInstaller-3.4. ...

  5. Ubuntu 18.04 配置 adb

    Reference1:  https://www.jianshu.com/p/377c6fb6e590 Reference2:  https://blog.csdn.net/ppggxn/articl ...

  6. Swift—UITextField的基本用法

    https://www.jianshu.com/p/63bdeca39ddf 1.文本输入框的创建##### let textField = UITextField(frame: CGRect(x:1 ...

  7. Hadoop家族系统学习路线

    本文主要介绍Hadoop家族产品,常用的项目包括Hadoop,Hive,Pig,HBase,Sqoop,Mahout,Zookeeper,Avro,Ambari,Chukwa,新增加的项目包括,YAR ...

  8. 装WIN7的一点心得

    一.为什么要装WIN7 长久以来个人的习惯,WIN10用不来,总体安装思路是:下官方版,找方法激活 二.安装镜像的来源 这个网上版本五花八门,各种系统网站,但都会有软件捆绑等行为,还有浏览器中强制捆了 ...

  9. bugku-杂项 听首音乐

    下载文件,是个wav文件,用Audacity打开,发现有 放大后记录下来:(每一组后面加上空格) ..... -... -.-. ----. ..--- ..... -.... ....- ----. ...

  10. c# 异步和同步 多线程

    在执行较为耗时的处理时,很容易出现用户界面“卡顿”现象,用异步编程模型,将耗时处理的代码放到另一个线程上执行,不会阻止用户界面线程的继续执行,应用程序 就不再出现“卡顿”现象. 本例子提供同步加载和异 ...