File tree 6 files changed +204
-4
lines changed
solution/0200-0299/0255.Verify Preorder Sequence in Binary Search Tree
6 files changed +204
-4
lines changed Original file line number Diff line number Diff line change 32
32
33
33
<p >您能否使用恒定的空间复杂度来完成此题?</p >
34
34
35
-
36
35
## 解法
37
36
38
37
<!-- 这里可写通用的实现逻辑 -->
39
38
39
+ 二叉搜索树先序遍历时,每次移向左子树时,值递减,移向右子树时,值递增。
40
+
41
+ 因此,可以维护一个单调递减栈。遍历序列,若当前值大于栈顶元素,说明开始要进入右子树的遍历。只要栈顶元素比当前值小,就表示还是左子树,要移除,也就是从栈中弹出,直至栈顶元素大于当前值,或者栈为空。此过程要记录弹出栈的最后一个元素 last。
42
+
43
+ 接下来继续往后遍历,之后右子树的每个节点,都要比 last 大,才能满足二叉搜索树,否则直接返回 false。
44
+
40
45
<!-- tabs:start -->
41
46
42
47
### ** Python3**
43
48
44
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
45
50
46
51
``` python
47
-
52
+ class Solution :
53
+ def verifyPreorder (self , preorder : List[int ]) -> bool :
54
+ stk = []
55
+ last = float (' -inf' )
56
+ for x in preorder:
57
+ if x < last:
58
+ return False
59
+ while stk and stk[- 1 ] < x:
60
+ last = stk.pop()
61
+ stk.append(x)
62
+ return True
48
63
```
49
64
50
65
### ** Java**
51
66
52
67
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
68
54
69
``` java
70
+ class Solution {
71
+ public boolean verifyPreorder (int [] preorder ) {
72
+ Deque<Integer > stk = new ArrayDeque<> ();
73
+ int last = Integer . MIN_VALUE ;
74
+ for (int x : preorder) {
75
+ if (x < last) {
76
+ return false ;
77
+ }
78
+ while (! stk. isEmpty() && stk. peek() < x) {
79
+ last = stk. poll();
80
+ }
81
+ stk. push(x);
82
+ }
83
+ return true ;
84
+ }
85
+ }
86
+ ```
87
+
88
+ ### ** C++**
89
+
90
+ ``` cpp
91
+ class Solution {
92
+ public:
93
+ bool verifyPreorder(vector<int >& preorder) {
94
+ stack<int > stk;
95
+ int last = INT_MIN;
96
+ for (int x : preorder)
97
+ {
98
+ if (x < last) return false;
99
+ while (!stk.empty() && stk.top() < x)
100
+ {
101
+ last = stk.top();
102
+ stk.pop();
103
+ }
104
+ stk.push(x);
105
+ }
106
+ return true;
107
+ }
108
+ };
109
+ ```
55
110
111
+ ### **Go**
112
+
113
+ ```go
114
+ func verifyPreorder(preorder []int) bool {
115
+ var stk []int
116
+ last := math.MinInt32
117
+ for _, x := range preorder {
118
+ if x < last {
119
+ return false
120
+ }
121
+ for len(stk) > 0 && stk[len(stk)-1] < x {
122
+ last = stk[len(stk)-1]
123
+ stk = stk[0 : len(stk)-1]
124
+ }
125
+ stk = append(stk, x)
126
+ }
127
+ return true
128
+ }
56
129
```
57
130
58
131
### ** ...**
Original file line number Diff line number Diff line change 33
33
<p >  ; </p >
34
34
<p ><strong >Follow up:</strong > Could you do it using only constant space complexity?</p >
35
35
36
-
37
36
## Solutions
38
37
39
38
<!-- tabs:start -->
40
39
41
40
### ** Python3**
42
41
43
42
``` python
44
-
43
+ class Solution :
44
+ def verifyPreorder (self , preorder : List[int ]) -> bool :
45
+ stk = []
46
+ last = float (' -inf' )
47
+ for x in preorder:
48
+ if x < last:
49
+ return False
50
+ while stk and stk[- 1 ] < x:
51
+ last = stk.pop()
52
+ stk.append(x)
53
+ return True
45
54
```
46
55
47
56
### ** Java**
48
57
49
58
``` java
59
+ class Solution {
60
+ public boolean verifyPreorder (int [] preorder ) {
61
+ Deque<Integer > stk = new ArrayDeque<> ();
62
+ int last = Integer . MIN_VALUE ;
63
+ for (int x : preorder) {
64
+ if (x < last) {
65
+ return false ;
66
+ }
67
+ while (! stk. isEmpty() && stk. peek() < x) {
68
+ last = stk. poll();
69
+ }
70
+ stk. push(x);
71
+ }
72
+ return true ;
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### ** C++**
78
+
79
+ ``` cpp
80
+ class Solution {
81
+ public:
82
+ bool verifyPreorder(vector<int >& preorder) {
83
+ stack<int > stk;
84
+ int last = INT_MIN;
85
+ for (int x : preorder)
86
+ {
87
+ if (x < last) return false;
88
+ while (!stk.empty() && stk.top() < x)
89
+ {
90
+ last = stk.top();
91
+ stk.pop();
92
+ }
93
+ stk.push(x);
94
+ }
95
+ return true;
96
+ }
97
+ };
98
+ ```
50
99
100
+ ### **Go**
101
+
102
+ ```go
103
+ func verifyPreorder(preorder []int) bool {
104
+ var stk []int
105
+ last := math.MinInt32
106
+ for _, x := range preorder {
107
+ if x < last {
108
+ return false
109
+ }
110
+ for len(stk) > 0 && stk[len(stk)-1] < x {
111
+ last = stk[len(stk)-1]
112
+ stk = stk[0 : len(stk)-1]
113
+ }
114
+ stk = append(stk, x)
115
+ }
116
+ return true
117
+ }
51
118
```
52
119
53
120
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool verifyPreorder (vector<int >& preorder) {
4
+ stack<int > stk;
5
+ int last = INT_MIN;
6
+ for (int x : preorder)
7
+ {
8
+ if (x < last) return false ;
9
+ while (!stk.empty () && stk.top () < x)
10
+ {
11
+ last = stk.top ();
12
+ stk.pop ();
13
+ }
14
+ stk.push (x);
15
+ }
16
+ return true ;
17
+ }
18
+ };
Original file line number Diff line number Diff line change
1
+ func verifyPreorder (preorder []int ) bool {
2
+ var stk []int
3
+ last := math .MinInt32
4
+ for _ , x := range preorder {
5
+ if x < last {
6
+ return false
7
+ }
8
+ for len (stk ) > 0 && stk [len (stk )- 1 ] < x {
9
+ last = stk [len (stk )- 1 ]
10
+ stk = stk [0 : len (stk )- 1 ]
11
+ }
12
+ stk = append (stk , x )
13
+ }
14
+ return true
15
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean verifyPreorder (int [] preorder ) {
3
+ Deque <Integer > stk = new ArrayDeque <>();
4
+ int last = Integer .MIN_VALUE ;
5
+ for (int x : preorder ) {
6
+ if (x < last ) {
7
+ return false ;
8
+ }
9
+ while (!stk .isEmpty () && stk .peek () < x ) {
10
+ last = stk .poll ();
11
+ }
12
+ stk .push (x );
13
+ }
14
+ return true ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def verifyPreorder (self , preorder : List [int ]) -> bool :
3
+ stk = []
4
+ last = float ('-inf' )
5
+ for x in preorder :
6
+ if x < last :
7
+ return False
8
+ while stk and stk [- 1 ] < x :
9
+ last = stk .pop ()
10
+ stk .append (x )
11
+ return True
You can’t perform that action at this time.
0 commit comments