Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 028d8ff

Browse files
Merge pull request #444 from xllpiupiu/master
0501二叉搜索树中的众数JavaScript版本
2 parents d6770c0 + 90100eb commit 028d8ff

File tree

3 files changed

+118
-111
lines changed

3 files changed

+118
-111
lines changed

‎problems/0235.二叉搜索树的最近公共祖先.md‎

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -313,65 +313,49 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
313313
}
314314
```
315315

316-
317-
JavaScript版本
318-
> 递归
319-
316+
JavaScript版本:
317+
1. 使用递归的方法
320318
```javascript
321-
/**
322-
* Definition for a binary tree node.
323-
* function TreeNode(val) {
324-
* this.val = val;
325-
* this.left = this.right = null;
326-
* }
327-
*/
328-
329-
/**
330-
* @param {TreeNode} root
331-
* @param {TreeNode} p
332-
* @param {TreeNode} q
333-
* @return {TreeNode}
334-
*/
335319
var lowestCommonAncestor = function(root, p, q) {
336-
if(root.val > p.val && root.val > q.val)
337-
return lowestCommonAncestor(root.left, p , q);
338-
else if(root.val < p.val && root.val < q.val)
339-
return lowestCommonAncestor(root.right, p , q);
320+
// 使用递归的方法
321+
// 1. 使用给定的递归函数lowestCommonAncestor
322+
// 2. 确定递归终止条件
323+
if(root === null) {
324+
return root;
325+
}
326+
if(root.val>p.val&&root.val>q.val) {
327+
// 向左子树查询
328+
let left = lowestCommonAncestor(root.left,p,q);
329+
return left !== null&&left;
330+
}
331+
if(root.val<p.val&&root.val<q.val) {
332+
// 向右子树查询
333+
let right = lowestCommonAncestor(root.right,p,q);
334+
return right !== null&&right;
335+
}
340336
return root;
341337
};
342338
```
343-
344-
> 迭代
345-
339+
2. 使用迭代的方法
346340
```javascript
347-
/**
348-
* Definition for a binary tree node.
349-
* function TreeNode(val) {
350-
* this.val = val;
351-
* this.left = this.right = null;
352-
* }
353-
*/
354-
355-
/**
356-
* @param {TreeNode} root
357-
* @param {TreeNode} p
358-
* @param {TreeNode} q
359-
* @return {TreeNode}
360-
*/
361341
var lowestCommonAncestor = function(root, p, q) {
362-
while(1) {
363-
if(root.val > p.val && root.val > q.val)
342+
// 使用迭代的方法
343+
while(root) {
344+
if(root.val>p.val&&root.val>q.val) {
364345
root = root.left;
365-
else if(root.val<p.val&&root.val<q.val)
346+
}else if(root.val<p.val&&root.val<q.val) {
366347
root = root.right;
367-
else
368-
break;
348+
}else {
349+
return root;
350+
}
351+
369352
}
370-
return root;
353+
return null;
371354
};
372355
```
373356

374357

358+
375359
-----------------------
376360
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
377361
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

‎problems/0236.二叉树的最近公共祖先.md‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -311,35 +311,34 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
311311
}
312312
```
313313

314-
JavaScript版本
315-
314+
JavaScript版本:
316315
```javascript
317-
/**
318-
* Definition for a binary tree node.
319-
* function TreeNode(val) {
320-
* this.val = val;
321-
* this.left = this.right = null;
322-
* }
323-
*/
324-
/**
325-
* @param {TreeNode} root
326-
* @param {TreeNode} p
327-
* @param {TreeNode} q
328-
* @return {TreeNode}
329-
*/
330316
var lowestCommonAncestor = function(root, p, q) {
331-
if(root === p || root === q || root === null)
332-
return root;
333-
let left = lowestCommonAncestor(root.left, p , q);
334-
let right = lowestCommonAncestor(root.right, p, q);
335-
if(left && right)
336-
return root;
337-
if(!left)
338-
return right;
339-
return left;
317+
// 使用递归的方法
318+
// 需要从下到上,所以使用后序遍历
319+
// 1. 确定递归的函数
320+
const travelTree = function(root,p,q) {
321+
// 2. 确定递归终止条件
322+
if(root === null || root === p||root === q) {
323+
return root;
324+
}
325+
// 3. 确定递归单层逻辑
326+
let left = travelTree(root.left,p,q);
327+
let right = travelTree(root.right,p,q);
328+
if(left !== null&&right !== null) {
329+
return root;
330+
}
331+
if(left ===null) {
332+
return right;
333+
}
334+
return left;
335+
}
336+
return travelTree(root,p,q);
340337
};
341338
```
342339

340+
341+
343342
-----------------------
344343
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
345344
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

‎problems/0501.二叉搜索树中的众数.md‎

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -523,52 +523,76 @@ func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计
523523
}
524524
```
525525

526-
JavaScript版本
527-
526+
JavaScript版本:
527+
使用额外空间map的方法:
528528
```javascript
529-
/**
530-
* Definition for a binary tree node.
531-
* function TreeNode(val, left, right) {
532-
* this.val = (val===undefined ? 0 : val)
533-
* this.left = (left===undefined ? null : left)
534-
* this.right = (right===undefined ? null : right)
535-
* }
536-
*/
537-
/**
538-
* @param {TreeNode} root
539-
* @return {number[]}
540-
*/
541-
var findMode = function (root) {
542-
let maxCount = 0;
543-
let curCount = 0;
544-
let pre = null;
529+
var findMode = function(root) {
530+
// 使用递归中序遍历
531+
let map = new Map();
532+
// 1. 确定递归函数以及函数参数
533+
const traverTree = function(root) {
534+
// 2. 确定递归终止条件
535+
if(root === null) {
536+
return ;
537+
}
538+
traverTree(root.left);
539+
// 3. 单层递归逻辑
540+
map.set(root.val,map.has(root.val)?map.get(root.val)+1:1);
541+
traverTree(root.right);
542+
}
543+
traverTree(root);
544+
//上面把数据都存储到map
545+
//下面开始寻找map里面的
546+
// 定义一个最大出现次数的初始值为root.val的出现次数
547+
let maxCount = map.get(root.val);
548+
// 定义一个存放结果的数组res
545549
let res = [];
546-
const inOrder = (root) => {
547-
if (root === null)
548-
return;
549-
inOrder(root.left);
550-
551-
if (pre === null)
552-
curCount = 1;
553-
else if (pre.val === root.val)
554-
curCount++;
555-
else
556-
curCount = 1;
557-
pre = root;
558-
559-
if (curCount === maxCount)
560-
res.push(root.val);
561-
562-
if (curCount > maxCount) {
563-
maxCount = curCount;
564-
res.splice(0, res.length);
565-
res.push(root.val);
550+
for(let [key,value] of map) {
551+
// 如果当前值等于最大出现次数就直接在res增加该值
552+
if(value === maxCount) {
553+
res.push(key);
566554
}
567-
568-
inOrder(root.right);
569-
return;
555+
// 如果value的值大于原本的maxCount就清空res的所有值,因为找到了更大的
556+
if(value>maxCount) {
557+
res = [];
558+
maxCount = value;
559+
res.push(key);
560+
}
561+
}
562+
return res;
563+
};
564+
```
565+
不使用额外空间,利用二叉树性质,中序遍历(有序):
566+
```javascript
567+
var findMode = function(root) {
568+
// 不使用额外空间,使用中序遍历,设置出现最大次数初始值为1
569+
let count = 0,maxCount = 1;
570+
let pre = root,res = [];
571+
// 1.确定递归函数及函数参数
572+
const travelTree = function(cur) {
573+
// 2. 确定递归终止条件
574+
if(cur === null) {
575+
return ;
576+
}
577+
travelTree(cur.left);
578+
// 3. 单层递归逻辑
579+
if(pre.val === cur.val) {
580+
count++;
581+
}else {
582+
count = 1;
583+
}
584+
pre = cur;
585+
if(count === maxCount) {
586+
res.push(cur.val);
587+
}
588+
if(count > maxCount) {
589+
res = [];
590+
maxCount = count;
591+
res.push(cur.val);
592+
}
593+
travelTree(cur.right);
570594
}
571-
inOrder(root);
595+
travelTree(root);
572596
return res;
573597
};
574598
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /