/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
int len = list.size();
for(int i = 0;i < len - 1;i++) {
if(list.get(i) >= list.get(i+1)) {
return false;
}
}
return true;
}
}
leetcode-java-98. Validate Binary Search Tree
最新推荐文章于 2022-09-08 08:57:02 发布