[Algo] 646. Store Number Of Nodes In Left Subtree
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的更多相关文章
- 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 ...
- 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 ...
- Kth Smallest Element in a BST 解答
Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...
- 【433】COMP9024 复习
目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...
- 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 ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- Storm 流式计算框架
1. 简介 是一个分布式, 高容错的 实时计算框架 Storm进程常驻内存, 永久运行 Storm数据不经过磁盘, 在内存中流转, 通过网络直接发送给下游 流式处理(streaming) 与 批处理( ...
- 【Python】【Django】查询所有学生信息
要做到以下效果: 改代码后效果: 从0开始顺序计数: 倒叙计数到0
- mybaits的注解方式与配置文件方式比较
注解方式比配置文件方式简单了更多. 俩种配置 一个是制定配置文件路径,一个直接class指向dao层接口 只需加一个注解@**** 就能够实现 比写一个xml的配置文件简单更多.
- tx2--开机启动
TX2上电自动开机 参考:http://121.42.13.250/?p=168 问题描述 Jetson TX2在接通电源后,按下板子上的PWOER BTN开机键(S4)后,便能够正常启动.但这对于一 ...
- 洛谷 P2543 [AHOI2004]奇怪的字符串
题目传送门 解题思路: 本题朴素求最长公共子序列即可,但是空间不够,怎么办呢? 空间不够,滚动数组来救 AC代码: #include<iostream> #include<cstdi ...
- HZNU-ACM寒假集训Day2小结 二分答案
Day2 ---二分 这里直接给出模板 两种对应不同的情况 可以借助数轴理解 int bsearch_1(int l, int r) { while (l < r) { ; if (check( ...
- linux中redis伪主从搭建
1.解压redis.tgz到usr/local/redis下 2.在redis/下执行 make 3.在redis/src/下执行 make install PREFIX=/usr/local/red ...
- 2020PHP面试-SQL篇
一.乐观锁和悲观锁 1.悲观锁是指假设并发更新会发生冲突,不管冲突是否会发生,都会使用锁机制. 优点: 完全保证数据安全. 缺点:锁机制会有额外开销,并发度降低. 可能会产生死锁. 2. 乐观锁是指假 ...
- spring boot2 运行环境
1.springboot个版本系统需求 spring boot maven jdk 内置tomcat 内置jetty servlet 2.0.x 3.2+ 8或9 8.5(3.1) 9.4(3.1) ...
- SUCTF 2019-EasySQL
0x00 知识点: 1:堆叠注入 2:sql_mode : 它定义了 MySQL 应支持的 SQL 语法,以及应该在数据上执行何种确认检查,其中的 PIPES_AS_CONCAT 将 || 视为字符串 ...