File tree Expand file tree Collapse file tree 5 files changed +162
-1
lines changed Expand file tree Collapse file tree 5 files changed +162
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ public int findNthDigit(int n) {
1010 count *= 10 ;
1111 start *= 10 ;
1212 }
13-
1413 start += (n - 1 ) / len ;
1514 String s = Integer .toString (start );
1615 return Character .getNumericValue (s .charAt ((n - 1 ) % len ));
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ // https://leetcode.com/problems/convert-bst-to-greater-tree/solution/
3+ // private int sum = 0;
4+
5+ // public TreeNode convertBST(TreeNode root) {
6+ // if (root != null) {
7+ // convertBST(root.right);
8+ // sum += root.val;
9+ // root.val = sum;
10+ // convertBST(root.left);
11+ // }
12+ // return root;
13+ // }
14+
15+ public TreeNode convertBST (TreeNode root ) {
16+ int sum = 0 ;
17+ TreeNode node = root ;
18+ Stack <TreeNode > stack = new Stack <TreeNode >();
19+
20+ while (!stack .isEmpty () || node != null ) {
21+ /* push all nodes up to (and including) this subtree's maximum on
22+ * the stack. */
23+ while (node != null ) {
24+ stack .add (node );
25+ node = node .right ;
26+ }
27+
28+ node = stack .pop ();
29+ sum += node .val ;
30+ node .val = sum ;
31+
32+ /* all nodes with values between the current and its parent lie in
33+ * the left subtree. */
34+ node = node .left ;
35+ }
36+
37+ return root ;
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * public class TreeNode {
4+ * int val;
5+ * TreeNode left;
6+ * TreeNode right;
7+ * TreeNode(int x) { val = x; }
8+ * }
9+ */
10+ public class Solution {
11+ public TreeNode mergeTrees (TreeNode t1 , TreeNode t2 ) {
12+ if (t1 == null )
13+ return t2 ;
14+ if (t2 == null )
15+ return t1 ;
16+ t1 .val += t2 .val ;
17+ t1 .left = mergeTrees (t1 .left , t2 .left );
18+ t1 .right = mergeTrees (t1 .right , t2 .right );
19+ return t1 ;
20+ }
21+
22+ // public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
23+ // if (t1 == null)
24+ // return t2;
25+ // Stack < TreeNode[] > stack = new Stack < > ();
26+ // stack.push(new TreeNode[] {t1, t2});
27+ // while (!stack.isEmpty()) {
28+ // TreeNode[] t = stack.pop();
29+ // if (t[0] == null || t[1] == null) {
30+ // continue;
31+ // }
32+ // t[0].val += t[1].val;
33+ // if (t[0].left == null) {
34+ // t[0].left = t[1].left;
35+ // } else {
36+ // stack.push(new TreeNode[] {t[0].left, t[1].left});
37+ // }
38+ // if (t[0].right == null) {
39+ // t[0].right = t[1].right;
40+ // } else {
41+ // stack.push(new TreeNode[] {t[0].right, t[1].right});
42+ // }
43+ // }
44+ // return t1;
45+ // }
46+ }
Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ # https://leetcode.com/problems/convert-bst-to-greater-tree/solution/
3+ # def __init__(self):
4+ # self.total = 0
5+
6+ # def convertBST(self, root):
7+ # if root is not None:
8+ # self.convertBST(root.right)
9+ # self.total += root.val
10+ # root.val = self.total
11+ # self.convertBST(root.left)
12+ # return root
13+
14+ def convertBST (self , root ):
15+ total = 0
16+
17+ node = root
18+ stack = []
19+ while stack or node is not None :
20+ # push all nodes up to (and including) this subtree's maximum on
21+ # the stack.
22+ while node is not None :
23+ stack .append (node )
24+ node = node .right
25+
26+ node = stack .pop ()
27+ total += node .val
28+ node .val = total
29+
30+ # all nodes with values between the current and its parent lie in
31+ # the left subtree.
32+ node = node .left
33+
34+ return root
Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode(object):
3+ # def __init__(self, x):
4+ # self.val = x
5+ # self.left = None
6+ # self.right = None
7+
8+ class Solution (object ):
9+ def mergeTrees (self , t1 , t2 ):
10+ """
11+ :type t1: TreeNode
12+ :type t2: TreeNode
13+ :rtype: TreeNode
14+ """
15+ if t1 is None :
16+ return t2
17+ if t2 is None :
18+ return t1
19+ t1 .val += t2 .val
20+ t1 .left = self .mergeTrees (t1 .left , t2 .left )
21+ t1 .right = self .mergeTrees (t1 .right , t2 .right )
22+ return t1
23+
24+ # def mergeTrees(self, t1, t2):
25+ # if t1 is None:
26+ # return t2
27+ # stack = [(t1, t2)]
28+ # while len(stack) != 0:
29+ # n1, n2 = stack.pop()
30+ # if n1 is None or n2 is None:
31+ # continue
32+ # n1.val += n2.val
33+ # if n1.left is None:
34+ # n1.left = n2.left
35+ # else:
36+ # stack.insert(0, (n1.left, n2.left))
37+ # if n1.right is None:
38+ # n1.right = n2.right
39+ # else:
40+ # stack.insert(0, (n1.right, n2.right))
41+ # return t1
You can’t perform that action at this time.
0 commit comments