Movatterモバイル変換


[0]ホーム

URL:


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

Commit2f9b3bb

Browse files
author
luzhipeng
committed
feat:azl397985856#92 增加注释 #236增加扩展 backlog增加题目
1 parent9782d62 commit2f9b3bb

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*@lc app=leetcode id=538 lang=javascript
3+
*
4+
* [538] Convert BST to Greater Tree
5+
*
6+
* https://leetcode.com/problems/convert-bst-to-greater-tree/description/
7+
*
8+
* algorithms
9+
* Easy (50.04%)
10+
* Total Accepted: 75.4K
11+
* Total Submissions: 149K
12+
* Testcase Example: '[5,2,13]'
13+
*
14+
* Given a Binary Search Tree (BST), convert it to a Greater Tree such that
15+
* every key of the original BST is changed to the original key plus sum of all
16+
* keys greater than the original key in BST.
17+
*
18+
*
19+
* Example:
20+
*
21+
* Input: The root of a Binary Search Tree like this:
22+
* ⁠ 5
23+
* ⁠ / \
24+
* ⁠ 2 13
25+
*
26+
* Output: The root of a Greater Tree like this:
27+
* ⁠ 18
28+
* ⁠ / \
29+
* ⁠ 20 13
30+
*
31+
*
32+
*/
33+
/**
34+
* Definition for a binary tree node.
35+
* function TreeNode(val) {
36+
* this.val = val;
37+
* this.left = this.right = null;
38+
* }
39+
*/
40+
/**
41+
*@param {TreeNode} root
42+
*@return {TreeNode}
43+
*/
44+
varconvertBST=function(root){
45+
letres=0;
46+
functionr(root){
47+
if(root===null)returnnull;
48+
49+
r(root.right);
50+
51+
root.val+=res;
52+
53+
res=+root.val;
54+
55+
r(root.left);
56+
57+
returnroot;
58+
}
59+
r(root);
60+
returnroot;
61+
};

‎problems/236.lowest-common-ancestor-of-a-binary-tree.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,6 @@ var lowestCommonAncestor = function(root, p, q) {
147147
};
148148
```
149149

150+
##扩展
151+
如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是嗯呢么意思,对结果有什么样的影响?
152+

‎problems/92.reverse-linked-list-ii.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ Output: 1->4->3->2->5->NULL
8989
*@return{ListNode}
9090
*/
9191
varreverseBetween=function(head,m,n) {
92+
// 虚拟节点,简化操作
9293
constdummyHead= {
9394
next: head
9495
}
9596

96-
let current=dummyHead.next;
97-
let pre= current;
98-
let index=0;
97+
let current=dummyHead.next;// 当前遍历的节点
98+
let pre= current;// 因为要反转,因此我们需要记住前一个节点
99+
let index=0;// 链表索引,用来判断是否是特殊位置(头尾位置)
99100

101+
// 上面提到的四个特殊节点
100102
let midStartNode=null;
101103
let preMidStartNode=null;
102104
let midEndNode=null;
@@ -106,11 +108,12 @@ var reverseBetween = function(head, m, n) {
106108
constnext=current.next;
107109
index++;
108110

109-
//reverse linked list
111+
//对 (m - n) 范围内的节点进行反转
110112
if (index> m&& index<= n) {
111113
current.next= pre;
112114
}
113115

116+
// 下面四个if都是边界, 用于更新四个特殊节点的值
114117
if (index=== m-1) {
115118
preMidStartNode= current;
116119
}
@@ -120,7 +123,6 @@ var reverseBetween = function(head, m, n) {
120123

121124
if (index=== n+1) {
122125
postMidEndNode= current;
123-
postMidEndNode.next
124126
}
125127

126128
if (index=== n) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp