【Leecode】207Validate Binary Search Tree有效二叉搜索树
来源:互联网 发布:淘宝品牌男装授权 编辑:程序博客网 时间:2024/06/04 23:36
/*
2.Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
判断一个二叉搜索树是否有效,左边子树的结点小于结点值,右边子树的结点大于结点值
思路:中序遍历
我们利用二分查找的性质,二分查找树的中序遍历是按照顺序递增的。所以我们只需要中序遍历一遍这棵树,维护前驱结点,每次检查是否满足递增关系即可。我们先中序遍历左孩子,直到没有左孩子,返回和前驱结点比较,然后递归这个节点的右孩子。即完成中序遍历过程。
Attention:
1. 前驱结点需要设置为引用类型,因为我们需要这个pre的值能够影响它在函数外部的值,即在各层递归中都同时改变。
bool in_order(TreeNode* node, TreeNode* &pre)
http://blog.csdn.net/cinderella_niu/article/details/43115689
class Solution {public: bool isValidBST(TreeNode* root) { TreeNode* pre = NULL; return middle_order(root,pre); }private: bool middle_order(TreeNode* node, TreeNode* &pre) { if(node == NULL) return true; if(!middle_order(node->left,pre)) return false; if(pre!=NULL && pre->val >= node->val) return false; pre = node; return middle_order(node->right, pre); }};
版本二:java代码,来自《程序员代码面试指南IT名企算法与数据结构题目最优解—左程云著》
/** * 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) { if (root == null) { return true; } boolean res = true; TreeNode pre = null; TreeNode cur1 = root; TreeNode cur2 = null; while (cur1 != null) { cur2 = cur1.left; if (cur2 != null) { while (cur2.right != null && cur2.right != cur1) { cur2 = cur2.right; } if (cur2.right == null) { cur2.right = cur1; cur1 = cur1.left; continue; } else { cur2.right = null; } } if (pre != null && pre.val >= cur1.val) { res = false; } pre = cur1; cur1 = cur1.right; } return res; }}
- 【Leecode】207Validate Binary Search Tree有效二叉搜索树
- LeetCode | Validate Binary Search Tree(有效的二叉搜索树)
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- leetCode 98.Validate Binary Search Tree (有效二叉搜索树) 解题思路和方法
- Validate Binary Search Tree 有效的二叉查找树
- 【判断二叉搜索树】Validate Binary Search Tree
- Validate Binary Search Tree 二叉搜索树的合法判断
- LeetCode--Validate Binary Search Tree(二叉搜索树)
- LeetCode(Validate Binary Search Tree) 判断一个二叉树是否是二叉搜索树
- 判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
- LeetCode OJ 之 Validate Binary Search Tree (验证二叉搜索树)
- 【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】
- 【LeetCode】98. Validate Binary Search Tree 解法,中序遍历,搜索二叉树合法性
- LeetCode 98. Validate Binary Search Tree(校验二叉搜索树)
- Leetcode 98. Validate Binary Search Tree 验证二叉搜索树 解题报告
- 98. Validate Binary Search Tree(判断合法二叉搜索树)
- LeetCode 98 Validate Binary Search Tree(判断二叉搜索树)
- Validate Binary Search Tree:检验一棵树是否是二叉搜索树
- unity网格合并后位置会变
- Intent传递数据
- The Marshtomp has seen it all before
- 可滑动Header控件的实现
- 【Dongle】【Java】基础(一)之输出
- 【Leecode】207Validate Binary Search Tree有效二叉搜索树
- swift LOG 输出
- (44)类的作用域
- C语言——分解质因数
- 关于SVM一篇比较全介绍的博文
- 有感
- Spring+Structs2+Hibernate实例
- Android动态加载之热修复与插件化
- HP大中华区总裁孙振耀退休感言