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 ...
随机推荐
- 销售行业ERP数据统计分析都有哪些维度?
场景描述 当前的企业信息化建设主要包括ERP系统.OA系统等.企业希望实现信息系统数据的整合,对企业资源进行分析汇总,方便对企业相关数据的掌控从而便于对业务流程进行及时调整监控. 但是由于系统间数据的 ...
- 苹果新的编程语言 Swift 语言进阶(九)--方法和下标
一.方法 方法是与特定类型相关的函数.与属性一样,方法也包括实例方法和类型方法. 类.结构.枚举都能定义实例方法,用来封装或实现给定类型的一个实例相关的功能或特定任务. 类.结构.枚举也能定义与类型本 ...
- SharePoint JS感悟-js脚本
最近有些迷恋js脚本,因为自己对Asp.net不够熟悉,又是Moss的一年级新生,不是对代码开发不感兴趣,面向对象自己也了解一些,代码也能大致读懂,个人觉得还是经验积累,作为代码开发人员,还是需要3- ...
- java--GUI(图形用户接口)
转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9795435 day22 01-GUI(概述) GUI(图形用户界面) 1. GUI(Griph ...
- 瑞芯微RK3188摄像头相关参数的配置
- jQuery插件学习基础
1.给jQuery添加全局的函数: $.zgz={ fn1:function(){ alert('我是刚设置的第一个全局函数') },fn2:function(){ alert('我是刚设置的第二个 ...
- Populating Next Right Pointers in Each Node(I and II)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 三种Tomcat集群方式的优缺点分析
三种Tomcat集群方式的优缺点分析 2009-09-01 10:00 kit_lo kit_lo的博客 字号:T | T 本文对三种Tomcat集群方式的优缺点进行了分析.三种集群方式分别是:使用D ...
- JVM笔记8-虚拟机性能监控与故障处理工具
1.JDK命令行工具 Java开发人员肯定都知道JDK的bin目录有“java.exe”,"javac.exe"这两个命令行工具,但并非所有程序员都了解过JDK的bin目录之中其他 ...
- 从__acrt_first_block == header 谈起,记录dll链接不一致的问题
最近写了一个postgresql的数据库连接池dll.写的比较随意,某个头文件如下: #pragma once #include "common.h"#include " ...