Skip to content

Commit ce68e5b

Browse files
committed
feat: add solutions to lc problem: No.1038
No.1038.Binary Search Tree to Greater Sum Tree
1 parent 695f930 commit ce68e5b

File tree

5 files changed

+483
-0
lines changed

5 files changed

+483
-0
lines changed

solution/1000-1099/1038.Binary Search Tree to Greater Sum Tree/README.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,191 @@ var bstToGst = function (root) {
420420
};
421421
```
422422

423+
### **TypeScript**
424+
425+
```ts
426+
/**
427+
* Definition for a binary tree node.
428+
* class TreeNode {
429+
* val: number
430+
* left: TreeNode | null
431+
* right: TreeNode | null
432+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
433+
* this.val = (val===undefined ? 0 : val)
434+
* this.left = (left===undefined ? null : left)
435+
* this.right = (right===undefined ? null : right)
436+
* }
437+
* }
438+
*/
439+
440+
function bstToGst(root: TreeNode | null): TreeNode | null {
441+
const dfs = (root: TreeNode | null, sum: number) => {
442+
if (root == null) {
443+
return sum;
444+
}
445+
const { val, left, right } = root;
446+
sum = dfs(right, sum) + val;
447+
root.val = sum;
448+
sum = dfs(left, sum);
449+
return sum;
450+
};
451+
dfs(root, 0);
452+
return root;
453+
}
454+
```
455+
456+
```ts
457+
/**
458+
* Definition for a binary tree node.
459+
* class TreeNode {
460+
* val: number
461+
* left: TreeNode | null
462+
* right: TreeNode | null
463+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
464+
* this.val = (val===undefined ? 0 : val)
465+
* this.left = (left===undefined ? null : left)
466+
* this.right = (right===undefined ? null : right)
467+
* }
468+
* }
469+
*/
470+
471+
function bstToGst(root: TreeNode | null): TreeNode | null {
472+
let cur = root;
473+
let sum = 0;
474+
while (cur != null) {
475+
const { val, left, right } = cur;
476+
if (right == null) {
477+
sum += val;
478+
cur.val = sum;
479+
cur = left;
480+
} else {
481+
let next = right;
482+
while (next.left != null && next.left != cur) {
483+
next = next.left;
484+
}
485+
if (next.left == null) {
486+
next.left = cur;
487+
cur = right;
488+
} else {
489+
next.left = null;
490+
sum += val;
491+
cur.val = sum;
492+
cur = left;
493+
}
494+
}
495+
}
496+
return root;
497+
}
498+
```
499+
500+
### **Rust**
501+
502+
```rust
503+
// Definition for a binary tree node.
504+
// #[derive(Debug, PartialEq, Eq)]
505+
// pub struct TreeNode {
506+
// pub val: i32,
507+
// pub left: Option<Rc<RefCell<TreeNode>>>,
508+
// pub right: Option<Rc<RefCell<TreeNode>>>,
509+
// }
510+
//
511+
// impl TreeNode {
512+
// #[inline]
513+
// pub fn new(val: i32) -> Self {
514+
// TreeNode {
515+
// val,
516+
// left: None,
517+
// right: None
518+
// }
519+
// }
520+
// }
521+
use std::rc::Rc;
522+
use std::cell::RefCell;
523+
impl Solution {
524+
fn dfs(root: &mut Option<Rc<RefCell<TreeNode>>>, mut sum: i32) -> i32 {
525+
if let Some(node) = root {
526+
let mut node = node.as_ref().borrow_mut();
527+
sum = Self::dfs(&mut node.right, sum) + node.val;
528+
node.val = sum;
529+
sum = Self::dfs(&mut node.left, sum);
530+
}
531+
sum
532+
}
533+
534+
pub fn bst_to_gst(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
535+
Self::dfs(&mut root, 0);
536+
root
537+
}
538+
}
539+
```
540+
541+
### **C**
542+
543+
```c
544+
/**
545+
* Definition for a binary tree node.
546+
* struct TreeNode {
547+
* int val;
548+
* struct TreeNode *left;
549+
* struct TreeNode *right;
550+
* };
551+
*/
552+
553+
554+
int dfs(struct TreeNode *root, int sum) {
555+
if (root) {
556+
sum = dfs(root->right, sum) + root->val;
557+
root->val = sum;
558+
sum = dfs(root->left, sum);
559+
}
560+
return sum;
561+
}
562+
563+
struct TreeNode *bstToGst(struct TreeNode *root) {
564+
dfs(root, 0);
565+
return root;
566+
}
567+
```
568+
569+
```c
570+
/**
571+
* Definition for a binary tree node.
572+
* struct TreeNode {
573+
* int val;
574+
* struct TreeNode *left;
575+
* struct TreeNode *right;
576+
* };
577+
*/
578+
579+
580+
struct TreeNode *bstToGst(struct TreeNode *root) {
581+
struct TreeNode *cur = root;
582+
int sum = 0;
583+
while (cur) {
584+
if (!cur->right) {
585+
sum += cur->val;
586+
cur->val = sum;
587+
cur = cur->left;
588+
} else {
589+
struct TreeNode *next = cur->right;
590+
while (next->left && next->left != cur) {
591+
next = next->left;
592+
}
593+
if (!next->left) {
594+
next->left = cur;
595+
cur = cur->right;
596+
} else {
597+
next->left = NULL;
598+
sum += cur->val;
599+
cur->val = sum;
600+
cur = cur->left;
601+
}
602+
}
603+
}
604+
return root;
605+
}
606+
```
607+
423608
### **...**
424609

425610
```

solution/1000-1099/1038.Binary Search Tree to Greater Sum Tree/README_EN.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,191 @@ var bstToGst = function (root) {
364364
};
365365
```
366366

367+
### **TypeScript**
368+
369+
```ts
370+
/**
371+
* Definition for a binary tree node.
372+
* class TreeNode {
373+
* val: number
374+
* left: TreeNode | null
375+
* right: TreeNode | null
376+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
377+
* this.val = (val===undefined ? 0 : val)
378+
* this.left = (left===undefined ? null : left)
379+
* this.right = (right===undefined ? null : right)
380+
* }
381+
* }
382+
*/
383+
384+
function bstToGst(root: TreeNode | null): TreeNode | null {
385+
const dfs = (root: TreeNode | null, sum: number) => {
386+
if (root == null) {
387+
return sum;
388+
}
389+
const { val, left, right } = root;
390+
sum = dfs(right, sum) + val;
391+
root.val = sum;
392+
sum = dfs(left, sum);
393+
return sum;
394+
};
395+
dfs(root, 0);
396+
return root;
397+
}
398+
```
399+
400+
```ts
401+
/**
402+
* Definition for a binary tree node.
403+
* class TreeNode {
404+
* val: number
405+
* left: TreeNode | null
406+
* right: TreeNode | null
407+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
408+
* this.val = (val===undefined ? 0 : val)
409+
* this.left = (left===undefined ? null : left)
410+
* this.right = (right===undefined ? null : right)
411+
* }
412+
* }
413+
*/
414+
415+
function bstToGst(root: TreeNode | null): TreeNode | null {
416+
let cur = root;
417+
let sum = 0;
418+
while (cur != null) {
419+
const { val, left, right } = cur;
420+
if (right == null) {
421+
sum += val;
422+
cur.val = sum;
423+
cur = left;
424+
} else {
425+
let next = right;
426+
while (next.left != null && next.left != cur) {
427+
next = next.left;
428+
}
429+
if (next.left == null) {
430+
next.left = cur;
431+
cur = right;
432+
} else {
433+
next.left = null;
434+
sum += val;
435+
cur.val = sum;
436+
cur = left;
437+
}
438+
}
439+
}
440+
return root;
441+
}
442+
```
443+
444+
### **Rust**
445+
446+
```rust
447+
// Definition for a binary tree node.
448+
// #[derive(Debug, PartialEq, Eq)]
449+
// pub struct TreeNode {
450+
// pub val: i32,
451+
// pub left: Option<Rc<RefCell<TreeNode>>>,
452+
// pub right: Option<Rc<RefCell<TreeNode>>>,
453+
// }
454+
//
455+
// impl TreeNode {
456+
// #[inline]
457+
// pub fn new(val: i32) -> Self {
458+
// TreeNode {
459+
// val,
460+
// left: None,
461+
// right: None
462+
// }
463+
// }
464+
// }
465+
use std::rc::Rc;
466+
use std::cell::RefCell;
467+
impl Solution {
468+
fn dfs(root: &mut Option<Rc<RefCell<TreeNode>>>, mut sum: i32) -> i32 {
469+
if let Some(node) = root {
470+
let mut node = node.as_ref().borrow_mut();
471+
sum = Self::dfs(&mut node.right, sum) + node.val;
472+
node.val = sum;
473+
sum = Self::dfs(&mut node.left, sum);
474+
}
475+
sum
476+
}
477+
478+
pub fn bst_to_gst(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
479+
Self::dfs(&mut root, 0);
480+
root
481+
}
482+
}
483+
```
484+
485+
### **C**
486+
487+
```c
488+
/**
489+
* Definition for a binary tree node.
490+
* struct TreeNode {
491+
* int val;
492+
* struct TreeNode *left;
493+
* struct TreeNode *right;
494+
* };
495+
*/
496+
497+
498+
int dfs(struct TreeNode *root, int sum) {
499+
if (root) {
500+
sum = dfs(root->right, sum) + root->val;
501+
root->val = sum;
502+
sum = dfs(root->left, sum);
503+
}
504+
return sum;
505+
}
506+
507+
struct TreeNode *bstToGst(struct TreeNode *root) {
508+
dfs(root, 0);
509+
return root;
510+
}
511+
```
512+
513+
```c
514+
/**
515+
* Definition for a binary tree node.
516+
* struct TreeNode {
517+
* int val;
518+
* struct TreeNode *left;
519+
* struct TreeNode *right;
520+
* };
521+
*/
522+
523+
524+
struct TreeNode *bstToGst(struct TreeNode *root) {
525+
struct TreeNode *cur = root;
526+
int sum = 0;
527+
while (cur) {
528+
if (!cur->right) {
529+
sum += cur->val;
530+
cur->val = sum;
531+
cur = cur->left;
532+
} else {
533+
struct TreeNode *next = cur->right;
534+
while (next->left && next->left != cur) {
535+
next = next->left;
536+
}
537+
if (!next->left) {
538+
next->left = cur;
539+
cur = cur->right;
540+
} else {
541+
next->left = NULL;
542+
sum += cur->val;
543+
cur->val = sum;
544+
cur = cur->left;
545+
}
546+
}
547+
}
548+
return root;
549+
}
550+
```
551+
367552
### **...**
368553

369554
```

0 commit comments

Comments
 (0)