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. DFS(深度优先搜索遍历求合格条件总数)--07--DFS--蓝桥杯方格填数

    此题方法多种,我用规范的DFS来求解 题目:方格填数 如下的10个格子,填入0~9的数字.要求:连续的两个数字不能相邻. (左右.上下.对角都算相邻)一共有多少种可能的填数方案?   输出 请填写表示 ...

  2. AIDL使用绑定启动远程Service出现Service Intent must be explicit: Intent

    Intent intent = new Intent(); intent.setAction("remote.MyRemoteService.Action"); 使用AIDL调用远 ...

  3. com.alibaba.druid.pool.DruidDataSource

    https://www.cnblogs.com/wuyun-blog/p/5679073.html DRUID介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PR ...

  4. Linux预习第三章节《重定向与管道符》20200219

  5. 微信小程序 -- 自定义抽屉式菜单(底部,从下向上拉出)

    实现一个抽屉菜单的案例 wxml <!--button--> <view class="btn" bindtap="powerDrawer" ...

  6. [Mathematics][Fundamentals of Complex Analysis][Small Trick] The Trick on drawing the picture of sin(z), for z in Complex Plane

    Exercises 3.2 21. (a). For $\omega = sinz$, what is the image of the semi-infinite strip $S_1 = \{x+ ...

  7. 第二阶段scrum-1

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 注册登陆界面功能正在实装,消息收发功能正在制作 雷达界面已经完成.

  8. idea中maven下载jar包不完整问题

    解决: 1.输入 mvn -U idea:idea 等1.都下载完毕后,在点击2.即Reimport

  9. UML-设计模式-对一组相关的对象使用抽象工厂模式

    1.场景 问题: javapos驱动,有2套,一套是IBM的,另一套是NCR的.如: 使用IBM硬件时要用IBM的驱动,使用NCR的硬件时要用NCR的驱动.那该如何设计呢? 注意,此处需要创建一组类( ...

  10. python数据拼接: pd.concat

    1.concat concat函数是在pandas底下的方法,可以将数据根据不同的轴作简单的融合 pd.concat(objs, axis=0, join='outer', join_axes=Non ...