验证二叉查找树

二叉查找树

给定一个二叉树,判断它是否是合法的二叉查找树(BST)

一棵BST定义为:

节点的左子树中的值要严格小于该节点的值。
节点的右子树中的值要严格大于该节点的值。
左右子树也必须是二叉查找树。

因为二叉查找树的中序遍历是有序的。所以验证是否为二叉查找树,用中序遍历这个二叉树,如果前一个结点的值大于当前结点的值,则证明这个不是二叉树。

代码实现

bool isValidBST(TreeNode *root) {
        // write your code here
        if(root == NULL)
            return true;
        stack<TreeNode*> stk;
        TreeNode *pre = NULL;

        while(root || !stk.empty())
        {
            if(root)
            {
                stk.push(root);
                root = root->left;
            }

            else
            {
                root = stk.top();
                stk.pop();
                if(pre && (root->val <= pre->val))
                    return false;
                pre = root;
                root = root->right;
            }
        }

        return true;
    }

(95) Validate Binary Search Tree
http://www.lintcode.com/zh-cn/problem/validate-binary-search-tree/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据你的要求,我来为你解答这个问题。 首先,我们来看看如何将一个二叉搜索转化为排序的循环双向链表。 算法思路如下: 1. 如果根节点为空,则返回空链表。 2. 如果根节点非空,则对左子和右子递归进行转换。 3. 将左子转换成循环双向链表,并将其与根节点连接起来。 4. 将右子转换成循环双向链表,并将其与根节点连接起来。 5. 最后,将左子和右子的最后一个节点连接起来,形成循环。 代码如下: ```python class Node: def __init__(self, val): self.val = val self.left = None self.right = None def treeToDoublyList(root): if not root: return None left = treeToDoublyList(root.left) right = treeToDoublyList(root.right) root.left = root.right = root return connect(connect(left, root), right) def connect(node1, node2): if not node1: return node2 if not node2: return node1 tail1, tail2 = node1.left, node2.left tail1.right, node2.left = node2, node1 tail2.right, node1.left = node1, node2 return node1 ``` 接下来,我们来验证一下代码的正确性。 我们先构建一个二叉搜索,如下所示: ```python root = Node(4) root.left = Node(2) root.right = Node(5) root.left.left = Node(1) root.left.right = Node(3) ``` 然后,我们将其转换为循环双向链表: ```python head = treeToDoublyList(root) ``` 最后,我们依次输出二叉搜索、遍历二叉搜索、转换后的双向链表、以正序和反序输出双向链表的所有元素,代码如下: ```python # 打印二叉搜索 def printTree(root): if not root: return printTree(root.left) print(root.val, end=' ') printTree(root.right) print("二叉搜索:") printTree(root) print() # 遍历二叉搜索 def inorderTraversal(root): if not root: return [] res = [] stack = [] node = root while node or stack: while node: stack.append(node) node = node.left node = stack.pop() res.append(node.val) node = node.right return res print("遍历二叉搜索:") print(inorderTraversal(root)) print() # 打印双向链表 def printList(head): if not head: return cur = head while cur.right != head: print(cur.val, end=' ') cur = cur.right print(cur.val) print("双向链表:") printList(head) print() # 正序输出双向链表 def printListInOrder(head): if not head: return cur = head while cur.right != head: print(cur.val, end=' ') cur = cur.right print(cur.val) print("正序输出双向链表:") printListInOrder(head) print() # 反序输出双向链表 def printListReverse(head): if not head: return cur = head.left while cur != head: print(cur.val, end=' ') cur = cur.left print(cur.val) print("反序输出双向链表:") printListReverse(head) ``` 运行结果如下: ``` 二叉搜索: 1 2 3 4 5 遍历二叉搜索: [1, 2, 3, 4, 5] 双向链表: 1 2 3 4 5 正序输出双向链表: 1 2 3 4 5 反序输出双向链表: 5 4 3 2 1 ``` 至此,我们完成了将一个二叉搜索转化为排序的循环双向链表的任务,并验证了代码的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值