mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #444 from xllpiupiu/master
0501二叉搜索树中的众数JavaScript版本
This commit is contained in:
@ -313,63 +313,47 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JavaScript版本:
|
||||||
JavaScript版本
|
1. 使用递归的方法
|
||||||
> 递归
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
/**
|
|
||||||
* Definition for a binary tree node.
|
|
||||||
* function TreeNode(val) {
|
|
||||||
* this.val = val;
|
|
||||||
* this.left = this.right = null;
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {TreeNode} root
|
|
||||||
* @param {TreeNode} p
|
|
||||||
* @param {TreeNode} q
|
|
||||||
* @return {TreeNode}
|
|
||||||
*/
|
|
||||||
var lowestCommonAncestor = function(root, p, q) {
|
var lowestCommonAncestor = function(root, p, q) {
|
||||||
if(root.val > p.val && root.val > q.val)
|
// 使用递归的方法
|
||||||
return lowestCommonAncestor(root.left, p , q);
|
// 1. 使用给定的递归函数lowestCommonAncestor
|
||||||
else if(root.val < p.val && root.val < q.val)
|
// 2. 确定递归终止条件
|
||||||
return lowestCommonAncestor(root.right, p , q);
|
if(root === null) {
|
||||||
return root;
|
return root;
|
||||||
};
|
}
|
||||||
```
|
if(root.val>p.val&&root.val>q.val) {
|
||||||
|
// 向左子树查询
|
||||||
> 迭代
|
let left = lowestCommonAncestor(root.left,p,q);
|
||||||
|
return left !== null&&left;
|
||||||
```javascript
|
}
|
||||||
/**
|
if(root.val<p.val&&root.val<q.val) {
|
||||||
* Definition for a binary tree node.
|
// 向右子树查询
|
||||||
* function TreeNode(val) {
|
let right = lowestCommonAncestor(root.right,p,q);
|
||||||
* this.val = val;
|
return right !== null&&right;
|
||||||
* this.left = this.right = null;
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {TreeNode} root
|
|
||||||
* @param {TreeNode} p
|
|
||||||
* @param {TreeNode} q
|
|
||||||
* @return {TreeNode}
|
|
||||||
*/
|
|
||||||
var lowestCommonAncestor = function(root, p, q) {
|
|
||||||
while(1) {
|
|
||||||
if(root.val > p.val && root.val > q.val)
|
|
||||||
root = root.left;
|
|
||||||
else if(root.val < p.val && root.val < q.val)
|
|
||||||
root = root.right;
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return root;
|
return root;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
2. 使用迭代的方法
|
||||||
|
```javascript
|
||||||
|
var lowestCommonAncestor = function(root, p, q) {
|
||||||
|
// 使用迭代的方法
|
||||||
|
while(root) {
|
||||||
|
if(root.val>p.val&&root.val>q.val) {
|
||||||
|
root = root.left;
|
||||||
|
}else if(root.val<p.val&&root.val<q.val) {
|
||||||
|
root = root.right;
|
||||||
|
}else {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -311,35 +311,34 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript版本
|
JavaScript版本:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
/**
|
|
||||||
* Definition for a binary tree node.
|
|
||||||
* function TreeNode(val) {
|
|
||||||
* this.val = val;
|
|
||||||
* this.left = this.right = null;
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* @param {TreeNode} root
|
|
||||||
* @param {TreeNode} p
|
|
||||||
* @param {TreeNode} q
|
|
||||||
* @return {TreeNode}
|
|
||||||
*/
|
|
||||||
var lowestCommonAncestor = function(root, p, q) {
|
var lowestCommonAncestor = function(root, p, q) {
|
||||||
if(root === p || root === q || root === null)
|
// 使用递归的方法
|
||||||
return root;
|
// 需要从下到上,所以使用后序遍历
|
||||||
let left = lowestCommonAncestor(root.left, p , q);
|
// 1. 确定递归的函数
|
||||||
let right = lowestCommonAncestor(root.right, p, q);
|
const travelTree = function(root,p,q) {
|
||||||
if(left && right)
|
// 2. 确定递归终止条件
|
||||||
return root;
|
if(root === null || root === p||root === q) {
|
||||||
if(!left)
|
return root;
|
||||||
return right;
|
}
|
||||||
return left;
|
// 3. 确定递归单层逻辑
|
||||||
|
let left = travelTree(root.left,p,q);
|
||||||
|
let right = travelTree(root.right,p,q);
|
||||||
|
if(left !== null&&right !== null) {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if(left ===null) {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
return travelTree(root,p,q);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
@ -523,52 +523,76 @@ func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript版本
|
JavaScript版本:
|
||||||
|
使用额外空间map的方法:
|
||||||
```javascript
|
```javascript
|
||||||
/**
|
var findMode = function(root) {
|
||||||
* Definition for a binary tree node.
|
// 使用递归中序遍历
|
||||||
* function TreeNode(val, left, right) {
|
let map = new Map();
|
||||||
* this.val = (val===undefined ? 0 : val)
|
// 1. 确定递归函数以及函数参数
|
||||||
* this.left = (left===undefined ? null : left)
|
const traverTree = function(root) {
|
||||||
* this.right = (right===undefined ? null : right)
|
// 2. 确定递归终止条件
|
||||||
* }
|
if(root === null) {
|
||||||
*/
|
return ;
|
||||||
/**
|
|
||||||
* @param {TreeNode} root
|
|
||||||
* @return {number[]}
|
|
||||||
*/
|
|
||||||
var findMode = function (root) {
|
|
||||||
let maxCount = 0;
|
|
||||||
let curCount = 0;
|
|
||||||
let pre = null;
|
|
||||||
let res = [];
|
|
||||||
const inOrder = (root) => {
|
|
||||||
if (root === null)
|
|
||||||
return;
|
|
||||||
inOrder(root.left);
|
|
||||||
|
|
||||||
if (pre === null)
|
|
||||||
curCount = 1;
|
|
||||||
else if (pre.val === root.val)
|
|
||||||
curCount++;
|
|
||||||
else
|
|
||||||
curCount = 1;
|
|
||||||
pre = root;
|
|
||||||
|
|
||||||
if (curCount === maxCount)
|
|
||||||
res.push(root.val);
|
|
||||||
|
|
||||||
if (curCount > maxCount) {
|
|
||||||
maxCount = curCount;
|
|
||||||
res.splice(0, res.length);
|
|
||||||
res.push(root.val);
|
|
||||||
}
|
}
|
||||||
|
traverTree(root.left);
|
||||||
inOrder(root.right);
|
// 3. 单层递归逻辑
|
||||||
return;
|
map.set(root.val,map.has(root.val)?map.get(root.val)+1:1);
|
||||||
|
traverTree(root.right);
|
||||||
}
|
}
|
||||||
inOrder(root);
|
traverTree(root);
|
||||||
|
//上面把数据都存储到map
|
||||||
|
//下面开始寻找map里面的
|
||||||
|
// 定义一个最大出现次数的初始值为root.val的出现次数
|
||||||
|
let maxCount = map.get(root.val);
|
||||||
|
// 定义一个存放结果的数组res
|
||||||
|
let res = [];
|
||||||
|
for(let [key,value] of map) {
|
||||||
|
// 如果当前值等于最大出现次数就直接在res增加该值
|
||||||
|
if(value === maxCount) {
|
||||||
|
res.push(key);
|
||||||
|
}
|
||||||
|
// 如果value的值大于原本的maxCount就清空res的所有值,因为找到了更大的
|
||||||
|
if(value>maxCount) {
|
||||||
|
res = [];
|
||||||
|
maxCount = value;
|
||||||
|
res.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
不使用额外空间,利用二叉树性质,中序遍历(有序):
|
||||||
|
```javascript
|
||||||
|
var findMode = function(root) {
|
||||||
|
// 不使用额外空间,使用中序遍历,设置出现最大次数初始值为1
|
||||||
|
let count = 0,maxCount = 1;
|
||||||
|
let pre = root,res = [];
|
||||||
|
// 1.确定递归函数及函数参数
|
||||||
|
const travelTree = function(cur) {
|
||||||
|
// 2. 确定递归终止条件
|
||||||
|
if(cur === null) {
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
travelTree(cur.left);
|
||||||
|
// 3. 单层递归逻辑
|
||||||
|
if(pre.val === cur.val) {
|
||||||
|
count++;
|
||||||
|
}else {
|
||||||
|
count = 1;
|
||||||
|
}
|
||||||
|
pre = cur;
|
||||||
|
if(count === maxCount) {
|
||||||
|
res.push(cur.val);
|
||||||
|
}
|
||||||
|
if(count > maxCount) {
|
||||||
|
res = [];
|
||||||
|
maxCount = count;
|
||||||
|
res.push(cur.val);
|
||||||
|
}
|
||||||
|
travelTree(cur.right);
|
||||||
|
}
|
||||||
|
travelTree(root);
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user