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

Commitd54d295

Browse files
authored
Update and rename Convert BST to greater tree.java to Convert BST to Greater Tree.java
1 parentf442fc8 commitd54d295

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

‎Easy/Convert BST to Greater Tree.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
classSolution {
17+
publicTreeNodeconvertBST(TreeNoderoot) {
18+
Stack<TreeNode>stack =newStack<>();
19+
addToStack(stack,root);
20+
intcurrSum =0;
21+
while (!stack.isEmpty()) {
22+
TreeNoderemoved =stack.pop();
23+
currSum +=removed.val;
24+
removed.val =currSum;
25+
addToStack(stack,removed.left);
26+
}
27+
returnroot;
28+
}
29+
30+
privatevoidaddToStack(Stack<TreeNode>stack,TreeNodenode) {
31+
while (node !=null) {
32+
stack.push(node);
33+
node =node.right;
34+
}
35+
}
36+
}

‎Easy/Convert BST to greater tree.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp