diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 95afe680..ccb63305 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -259,6 +259,36 @@ public: ## Java +```Java +//使用統一迭代法 +class Solution { + public boolean isValidBST(TreeNode root) { + Stack stack = new Stack<>(); + TreeNode pre = null; + if(root != null) + stack.add(root); + while(!stack.isEmpty()){ + TreeNode curr = stack.peek(); + if(curr != null){ + stack.pop(); + if(curr.right != null) + stack.add(curr.right); + stack.add(curr); + stack.add(null); + if(curr.left != null) + stack.add(curr.left); + }else{ + stack.pop(); + TreeNode temp = stack.pop(); + if(pre != null && pre.val >= temp.val) + return false; + pre = temp; + } + } + return true; + } +} +``` ```Java class Solution { // 递归