LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits
Digit root 数根问题
/**
* @param {number} num
* @return {number}
*/
var addDigits = function(num) {
var b = (num-1) % 9 + 1 ;
return b;
}; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同。
性质说明
1.任何数加9的数字根还是它本身。(特殊情况num=0)
2.9乘任何数字的数字根都是9。
104. Maximum Depth of Binary Tree
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if(root===null){
return 0;
}
var le = maxDepth(root.left);
var ri = maxDepth(root.right); return 1+Math.max(le,ri); };
【注】custom test里面的binary tree visualizer使用数组产生二叉树,如[1,2,3,4,5]
这题考察二叉树深度的计算,使用遍历完成,从底层return0不断+1到初始位置完成计算
226. Invert Binary Tree
题目描述里面的这句话笑了- -: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root===null){
return root
} var tem = root.left;
root.left = root.right;
root.right = tem;
invertTree(root.left);
invertTree(root.right); return root
};
【注】这题和上面的二叉树深度的题有点像,一下就做出来了,没什么好说的
LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree的更多相关文章
- [LeetCode]题解(python):104 Maximum Depth of Binary Tree
题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maxim ...
- [LeetCode&Python] Problem 258. Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
随机推荐
- Android群英传笔记——第四章:ListView使用技巧
Android群英传笔记--第四章:ListView使用技巧 最近也是比较迷茫,但是有一点点还是要坚持的,就是学习了,最近离职了,今天也是继续温习第四章ListView,也拖了其实也挺久的了,list ...
- Gradle 1.12用户指南翻译——第四十章. ANTLR 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- Windows平台安装及配置Hadoop(不借助cygwin)
由于项目需要,我在VMware上装了几个虚拟机Windows server 2012 R2,并要搭建Hadoop集群.刚刚入门hadoop,一头雾水,然后开始搜各种教程,首先是选用cygwin进行安装 ...
- 自动布局Autoresizing与Autolayout
一.关于iPhone屏幕的一些基本常识 1.ios屏幕适配的尺寸 iPhone的尺寸3.5inch.4.0inch.4.7inch.5.5inch iPad的尺寸7.9inch.9.7inch 2.点 ...
- ]Java 5|6 并发包介绍
ava.util.concurrent 包含许多线程安全.测试良好.高性能的并发构建块.不客气地说,创建 java.util.concurrent 的目的就是要实现 Collection 框架对数据结 ...
- P3370 【模板】字符串哈希
题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 输入输出格式 输入格式: 第一行包含一个整数N,为字符串 ...
- 关于mybatis更新数据的问题
前两天用mybatis的时候,发现这样一个问题,日志显示mytatis更新数据已经成功了,但是实际上数据库是没有更新到的,经过一番查找,发现mybatis更新的时候默认返回的是查找到的数据(Rows ...
- FFPLAY的原理(四)
意外情况 你们将会注意到我们有一个全局变量quit,我们用它来保证还没有设置程序退出的信号(SDL会自动处理TERM类似的信号).否则,这个线程将不停地运 行直到我们使用kill -9来结束程序.FF ...
- Greenplum测试部署笔记
按照官方Readme文档在Ubunut16.04上成功编译安装Greenplum最新代码(now:2017-11-12 21:40) 按照文档安装的过程中主要出现两个问题: 1.Root用户安装会卡在 ...
- /usr/lib/uwsgi/plugins/python_plugin.so: cannot open shared object file: No such file or directory
Django uwsgi部署方式下产生这个Bug,后来发现把uwsgi配置ini文件里面的 #plugins = python 把上面这句配置语句注释掉,uwsgi就可以运行了,当然,是正常可用运行状 ...