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 ...
随机推荐
- ITU-T Technical Paper: NP, QoS 和 QoE的框架以及它们的区别
本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...
- Linux配置文件系统及程序的限制 - ulimit
想象一个状况:我的 Linux 主机里面同时登陆了十个人,这十个人不知怎么搞的, 同时开启了 100 个文件,每个文件的大小约 10MBytes ,请问一下, 我的 Linux 主机的内存要有多大才够 ...
- C++实现二叉树
#include <iostream> using namespace std ; class Tree { public : int number ; class Tree *left ...
- 关于GPL329A添加摄像头驱动需要更改的配置脚本
我今天要添加一个ov2685的驱动进Digogo这部机子,当然要让它开机自动启动,就要想办法让它的.ko在启动文件系统的时候要自动被装载,这样上层打开摄像头才能加载摄像头驱动. 我找到源码工程对应添加 ...
- LeetCode之旅(20)-Power of Three
题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...
- asp.net core选项配置的研究
asp.net-core选项模块是全新,可拓展的框架,其作用在整个.net-core框架中,就像依赖注入一样无处不在,是一个很重要的组件. 其实配置模块与选项模块是紧密相连的,我们可以使用Config ...
- linux 下使用 tc 模拟网络延迟和丢包
1 模拟延迟传输简介 netem 与 tc: netem 是 Linux 2.6 及以上内核版本提供的一个网络模拟功能模块.该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带 ...
- 代理网络中安装tomcat的注意事项
搭建J2EE开发环境的时候,tomcat怎么都没办法访问主页面.主要的问题就是Network Error (tcp_error) 百度了半天也没搞明白,最后没办法,打算重装tomcat,便对照完整的安 ...
- JSP指令与动作
Jsp基本指令和动作 (2011-08-18 16:25:13) 转载▼ 标签: 杂谈 分类: java JSP基本指令 jsp命令指令用来设置与整个jsp页面相关的属性,它并不直接产生任何可见的输出 ...
- 记住这个网站:服务器相关数据统计网站 http://news.netcraft.com/
http://news.netcraft.com/ 需要参考现在服务器相关数据,可以上这个网站. 当然google趋势也是一个可选得备案. 有一个数据统计更全面的: http://w3techs.co ...