forked from sl1673495/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/sl1673495/leetcode-javasc…
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var kthSmallest = function (root, k) { | ||
let count = 0 | ||
let finded | ||
|
||
let dfs = (node) => { | ||
if (!node) { | ||
return | ||
} | ||
dfs(node.left) | ||
count++ | ||
if (count === k) { | ||
finded = node.val | ||
return | ||
} | ||
dfs(node.right) | ||
} | ||
|
||
dfs(root) | ||
|
||
return finded | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
let minDepth = function (root) { | ||
if (!root) return 0 | ||
|
||
let depth = 0 | ||
let queue = [root] | ||
|
||
while (queue.length) { | ||
depth++ | ||
let len = queue.length | ||
while (len--) { | ||
let node = queue.shift() | ||
|
||
let left = node.left | ||
let right = node.right | ||
if (!left && !right) { | ||
return depth | ||
} | ||
|
||
if (left) { | ||
queue.push(left) | ||
} | ||
if (right) { | ||
queue.push(right) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
let deleteNode = function (root, key) { | ||
let findNodePos = (node, key) => { | ||
if (!node) { | ||
return false | ||
} | ||
if (node.left && node.left.val === key) { | ||
return { | ||
parent: node, | ||
pos: "left", | ||
} | ||
} else if (node.right && node.right.val === key) { | ||
return { | ||
parent: node, | ||
pos: "right", | ||
} | ||
} else { | ||
return findNodePos(node.left, key) || findNodePos(node.right, key) | ||
} | ||
} | ||
|
||
let findLastLeft = (node) => { | ||
if (!node.left) { | ||
return node | ||
} | ||
return findLastLeft(node.left) | ||
} | ||
|
||
let virtual = new TreeNode() | ||
virtual.left = root | ||
|
||
let finded = findNodePos(virtual, key) | ||
if (finded) { | ||
let { parent, pos } = finded | ||
let target = parent[pos] | ||
let targetLeft = target.left | ||
let targetRight = target.right | ||
|
||
if (!targetLeft && !targetRight) { | ||
parent[pos] = null | ||
} else if (!targetRight) { | ||
parent[pos] = targetLeft | ||
} else if (!targetLeft) { | ||
parent[pos] = targetRight | ||
} else { | ||
parent[pos] = targetRight | ||
let lastLeft = findLastLeft(targetRight) | ||
lastLeft.left = targetLeft | ||
} | ||
} | ||
|
||
return virtual.left | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
let sortedArrayToBST = function (nums) { | ||
let n = nums.length | ||
if (!n) { | ||
return null | ||
} | ||
let mid = Math.floor(n / 2) | ||
let root = new TreeNode(nums[mid]) | ||
|
||
root.left = sortedArrayToBST(nums.slice(0, mid)) | ||
root.right = sortedArrayToBST(nums.slice(mid + 1, n)) | ||
|
||
return root | ||
}; |