国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

98. Validate Binary Search Tree(判斷合法二叉搜索樹)

2019-11-14 09:25:40
字體:
來源:轉載
供稿:網友
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 3Binary tree [2,1,3], return true.Example 2: 1 / / 2 3Binary tree [1,2,3], return false.

我在這個問題上犯了個錯誤,一開始我僅僅把二叉樹三個節點對比大小,可能造成二叉樹局部三個節點符合BST樹特性,但是放在全局就不符合了。因此我們要記錄min_node和max_node,從頂層遞歸到下層。而不是從下層開始,僅僅因為三個節點滿足就返回true。

典型情況:

10 / / 4 15 / / / /2 5 6 17

如上圖,15,6,17局部滿足BST樹,但是6<10,所以不是BST樹。

我的錯誤解法

class Solution {public: bool isValidBST(TreeNode* root) { return root != NULL ? is_bst(root) : true; } bool is_bst(TreeNode* root){ if(root->left == NULL && root->right == NULL) return true; else if(root->left == NULL) return is_bst(root->right); else if(root->right == NULL) return is_bst(root->left); else return is_bst(root->left) && is_bst(root->right) && (root->left->val <= root->val && root->right->val > root->val); }};

實際上錯誤解法通過了80%的case。

下面說正確解法方法一: 利用BST樹的特性,從上往下遞歸,記錄min_node和max_node,對左子樹來說,只需記錄max_node,即它的父節點;同理對于右字數,只需記錄min_node。然后它們滿足BST關系即可。

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isValidBST(TreeNode* root) { return root != NULL ? is_bst(root, NULL, NULL) : true; } bool is_bst(TreeNode* root, TreeNode* min_node, TreeNode* max_node){ if(root == NULL) return true; if(min_node != NULL && root->val <= min_node->val || max_node != NULL && root->val >= max_node->val) return false; //if false, stop and return return is_bst(root->left, min_node, root) && is_bst(root->right, root, max_node); }};

方法二:利用中序遍歷關系。由于BST樹的中序遍歷是有序的,所以我們用中序遍歷來做文章。

class Solution {public: bool isValidBST(TreeNode* root) { TreeNode* PRev = NULL; return is_bst(root, prev); } bool is_bst(TreeNode* root, TreeNode*& prev){ if(root == NULL) return true; if(!is_bst(root->left, prev)) return false; if(prev != NULL && prev->val >= root->val) return false; prev = root; return is_bst(root->right, prev); }};

利用prev節點一開始為NULL,后來作為中序遍歷的前一個節點,和當前節點進行比較判斷是否滿足BST特性即可。

唉,人生苦短,我用Python :)

# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def isValidBST(self, root): self.prev = None return self.is_bst(root, self.prev) def is_bst(self, root, prev): if root == None: return True if not self.is_bst(root.left, self.prev): return False if self.prev != None and self.prev.val >= root.val: return False self.prev = root return self.is_bst(root.right, self.prev)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 耿马| 肇源县| 嵊州市| 荆门市| 湄潭县| 马鞍山市| 沙湾县| 新津县| 定陶县| 文登市| 晋江市| 金沙县| 元朗区| 温宿县| 泰宁县| 湘西| 定兴县| 常熟市| 江孜县| 景谷| 长寿区| 临夏县| 镇雄县| 沧州市| 淮安市| 仁怀市| 大连市| 曲沃县| 曲松县| 柏乡县| 高陵县| 江陵县| 六盘水市| 长春市| 洛阳市| 宿松县| 安平县| 瑞丽市| 文成县| 武邑县| 广宁县|