leetcode-java-98. Validate Binary Search Tree

来源:互联网 发布:淘宝优惠券返利能赚钱 编辑:程序博客网 时间:2024/05/15 05:57
/** * 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;     } }
0 0
原创粉丝点击