File tree 6 files changed +172
-4
lines changed
solution/0800-0899/0897.Increasing Order Search Tree
6 files changed +172
-4
lines changed Original file line number Diff line number Diff line change 31
31
32
32
递归将左子树、右子树转换为左、右链表 left 和 right。然后将左链表 left 的最后一个结点的 right 指针指向 root,root 的 right 指针指向右链表 right,并将 root 的 left 指针值为空。
33
33
34
+ 同 [ 897. 递增顺序查找树] ( /solution/0800-0899/0897.Increasing%20Order%20Search%20Tree/README.md ) 。
35
+
34
36
<!-- tabs:start -->
35
37
36
38
### ** Python3**
Original file line number Diff line number Diff line change 30
30
31
31
## Solutions
32
32
33
+ See [ 897. Increasing Order Search Tree] ( /solution/0800-0899/0897.Increasing%20Order%20Search%20Tree/README_EN.md ) .
34
+
33
35
<!-- tabs:start -->
34
36
35
37
### ** Python3**
Original file line number Diff line number Diff line change 54
54
55
55
<!-- 这里可写通用的实现逻辑 -->
56
56
57
+ 递归将左子树、右子树转换为左、右链表 left 和 right。然后将左链表 left 的最后一个结点的 right 指针指向 root,root 的 right 指针指向右链表 right,并将 root 的 left 指针值为空。
58
+
59
+ 同[ 面试题 17.12. BiNode] ( /lcci/17.12.BiNode/README.md ) 。
60
+
57
61
<!-- tabs:start -->
58
62
59
63
### ** Python3**
60
64
61
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
66
63
67
``` python
64
-
68
+ # Definition for a binary tree node.
69
+ # class TreeNode:
70
+ # def __init__(self, val=0, left=None, right=None):
71
+ # self.val = val
72
+ # self.left = left
73
+ # self.right = right
74
+ class Solution :
75
+ def increasingBST (self , root : TreeNode) -> TreeNode:
76
+ if root is None :
77
+ return None
78
+ left = self .increasingBST(root.left)
79
+ right = self .increasingBST(root.right)
80
+ if left is None :
81
+ root.right = right
82
+ return root
83
+ res = left
84
+ while left and left.right:
85
+ left = left.right
86
+ left.right = root
87
+ root.right = right
88
+ root.left = None
89
+ return res
65
90
```
66
91
67
92
### ** Java**
68
93
69
94
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
95
71
96
``` java
72
-
97
+ /**
98
+ * Definition for a binary tree node.
99
+ * public class TreeNode {
100
+ * int val;
101
+ * TreeNode left;
102
+ * TreeNode right;
103
+ * TreeNode() {}
104
+ * TreeNode(int val) { this.val = val; }
105
+ * TreeNode(int val, TreeNode left, TreeNode right) {
106
+ * this.val = val;
107
+ * this.left = left;
108
+ * this.right = right;
109
+ * }
110
+ * }
111
+ */
112
+ class Solution {
113
+ public TreeNode increasingBST (TreeNode root ) {
114
+ if (root == null ) return null ;
115
+ TreeNode left = increasingBST(root. left);
116
+ TreeNode right = increasingBST(root. right);
117
+ if (left == null ) {
118
+ root. right = right;
119
+ return root;
120
+ }
121
+ TreeNode res = left;
122
+ while (left != null && left. right != null ) left = left. right;
123
+ left. right = root;
124
+ root. right = right;
125
+ root. left = null ;
126
+ return res;
127
+ }
128
+ }
73
129
```
74
130
75
131
### ** ...**
Original file line number Diff line number Diff line change 47
47
48
48
## Solutions
49
49
50
+ See [ 17.12. BiNode] ( /lcci/17.12.BiNode/README_EN.md ) .
51
+
50
52
<!-- tabs:start -->
51
53
52
54
### ** Python3**
53
55
54
56
``` python
55
-
57
+ # Definition for a binary tree node.
58
+ # class TreeNode:
59
+ # def __init__(self, val=0, left=None, right=None):
60
+ # self.val = val
61
+ # self.left = left
62
+ # self.right = right
63
+ class Solution :
64
+ def increasingBST (self , root : TreeNode) -> TreeNode:
65
+ if root is None :
66
+ return None
67
+ left = self .increasingBST(root.left)
68
+ right = self .increasingBST(root.right)
69
+ if left is None :
70
+ root.right = right
71
+ return root
72
+ res = left
73
+ while left and left.right:
74
+ left = left.right
75
+ left.right = root
76
+ root.right = right
77
+ root.left = None
78
+ return res
56
79
```
57
80
58
81
### ** Java**
59
82
60
83
``` java
61
-
84
+ /**
85
+ * Definition for a binary tree node.
86
+ * public class TreeNode {
87
+ * int val;
88
+ * TreeNode left;
89
+ * TreeNode right;
90
+ * TreeNode() {}
91
+ * TreeNode(int val) { this.val = val; }
92
+ * TreeNode(int val, TreeNode left, TreeNode right) {
93
+ * this.val = val;
94
+ * this.left = left;
95
+ * this.right = right;
96
+ * }
97
+ * }
98
+ */
99
+ class Solution {
100
+ public TreeNode increasingBST (TreeNode root ) {
101
+ if (root == null ) return null ;
102
+ TreeNode left = increasingBST(root. left);
103
+ TreeNode right = increasingBST(root. right);
104
+ if (left == null ) {
105
+ root. right = right;
106
+ return root;
107
+ }
108
+ TreeNode res = left;
109
+ while (left != null && left. right != null ) left = left. right;
110
+ left. right = root;
111
+ root. right = right;
112
+ root. left = null ;
113
+ return res;
114
+ }
115
+ }
62
116
```
63
117
64
118
### ** ...**
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() {}
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
+ class Solution {
17
+ public TreeNode increasingBST (TreeNode root ) {
18
+ if (root == null ) return null ;
19
+ TreeNode left = increasingBST (root .left );
20
+ TreeNode right = increasingBST (root .right );
21
+ if (left == null ) {
22
+ root .right = right ;
23
+ return root ;
24
+ }
25
+ TreeNode res = left ;
26
+ while (left != null && left .right != null ) left = left .right ;
27
+ left .right = root ;
28
+ root .right = right ;
29
+ root .left = null ;
30
+ return res ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode:
3
+ # def __init__(self, val=0, left=None, right=None):
4
+ # self.val = val
5
+ # self.left = left
6
+ # self.right = right
7
+ class Solution :
8
+ def increasingBST (self , root : TreeNode ) -> TreeNode :
9
+ if root is None :
10
+ return None
11
+ left = self .increasingBST (root .left )
12
+ right = self .increasingBST (root .right )
13
+ if left is None :
14
+ root .right = right
15
+ return root
16
+ res = left
17
+ while left and left .right :
18
+ left = left .right
19
+ left .right = root
20
+ root .right = right
21
+ root .left = None
22
+ return res
You can’t perform that action at this time.
0 commit comments