-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
3 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
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,31 @@ | ||
/* | ||
* @lc app=leetcode.cn id=144 lang=javascript | ||
* | ||
* [144] 二叉树的前序遍历 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* 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 | ||
* @return {number[]} | ||
*/ | ||
// 递归 | ||
var preorderTraversal = function(root, arr = []) { | ||
if(root) { | ||
// 根 -> 左 -> 右 | ||
arr.push(root.val) | ||
preorderTraversal(root.left, arr) | ||
preorderTraversal(root.right, arr) | ||
} | ||
return arr | ||
}; | ||
// @lc code=end | ||
|
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,31 @@ | ||
/* | ||
* @lc app=leetcode.cn id=145 lang=javascript | ||
* | ||
* [145] 二叉树的后序遍历 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* 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 | ||
* @return {number[]} | ||
*/ | ||
// 递归 | ||
var postorderTraversal = function(root, arr = []) { | ||
if(root) { | ||
// 左 -> 右 -> 根 | ||
postorderTraversal(root.left, arr) | ||
postorderTraversal(root.right, arr) | ||
arr.push(root.val) | ||
} | ||
return arr | ||
}; | ||
// @lc code=end | ||
|
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,40 @@ | ||
/* | ||
* @lc app=leetcode.cn id=50 lang=javascript | ||
* | ||
* [50] Pow(x, n) | ||
*/ | ||
// 题目:https://leetcode.cn/problems/powx-n/ | ||
// @lc code=start | ||
/** | ||
* @param {number} x | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
// 1、调用库函数:时间复杂度:O(1) | ||
var myPow = function(x, n) { | ||
return Math.pow(x, n); | ||
} | ||
// 2、暴力法:时间复杂度:O(n) | ||
var myPow = function(x, n) { | ||
if(n < 0) { | ||
x = 1/x; | ||
n = -n; | ||
} | ||
let result = 1; | ||
for(let i = 0;i < n;i++) { | ||
result = result * x; | ||
} | ||
return result; | ||
} | ||
// 3、递归+分治:时间复杂度:O(logn) | ||
var myPow = function(x, n) { | ||
if(n === 0) return 1;// n为0直接返回1 | ||
if(n < 0) {// n是负数 | ||
return 1/myPow(x,-n); | ||
} | ||
if(n%2) {//n是奇数 | ||
return x*myPow(x,n-1); | ||
} | ||
return myPow(x*x,n/2) //n是偶数,使用分治,一分为二,等于x*x的n/2次方 | ||
}; | ||
// @lc code=end |
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,31 @@ | ||
/* | ||
* @lc app=leetcode.cn id=94 lang=javascript | ||
* | ||
* [94] 二叉树的中序遍历 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* 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 | ||
* @return {number[]} | ||
*/ | ||
// 递归 | ||
var inorderTraversal = function(root, arr = []) { | ||
if(root) { | ||
// 左 -> 根 -> 右 | ||
inorderTraversal(root.left, arr) | ||
arr.push(root.val) | ||
inorderTraversal(root.right, arr) | ||
} | ||
return arr | ||
}; | ||
// @lc code=end | ||
|