Validate Binary Search
这道题很简单,就是中序遍历,得到list然后valid数字排序即可
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: True if the binary tree is BST, or false */ public boolean isValidBST(TreeNode root) { // write your code here ArrayList<Integer> rst = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode curNode = root; while (curNode != null || !stack.empty()) { while (curNode != null) { stack.push(curNode); curNode = curNode.left; } curNode = stack.peek(); stack.pop(); rst.add(curNode.val); curNode = curNode.right; } for (int i = 1; i < rst.size(); i++) { if (rst.get(i) <= rst.get(i -1)) { return false; } } return true; } }
posted on 2017-03-15 03:56 codingEskimo 阅读(101) 评论(0) 收藏 举报
浙公网安备 33010602011771号