LeetCode | 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.
題目解析:
判斷是否為二叉搜尋樹。其中的結點不能相等。
方案一:
既然遞迴麻煩,就利用二叉搜尋樹的性質,中序遍歷得到有序的結果。然後再判斷是否前一個值小於後一個,如果不滿足就返回false。
class Solution { public: bool isValidBST(TreeNode *root) { if(root == NULL) return true; InOrder(root); for(int i = 0;i < arr.size()-1;i++){ if(arr[i]>=arr[i+1]) return false; } return true; } void InOrder(TreeNode *root){ if(root == NULL) return; InOrder(root->left); arr.push_back(root->val); InOrder(root->right); } private: vector<int> arr; };
方案二:
可以通過遞迴的形式,但我們用從下往上遞迴還是從上往下遞迴呢?
先來看看從下往上遞迴,遞迴到深層次後,函式返回最大值和最小值,然後根據從左子樹返回還是右子樹返回,與根判斷。並更新最大值最小值再返回到更上一層。
這個思路挺麻煩,實現起來也要有很多條件判斷。那麼從上往下呢?我們遞迴到深層次的時候,先讓根與上層傳遞的上下界判斷,如果不在這個界內,就立即返回FALSE。更深層次的時候,要用當前的根來更新相應的上界或者下屆。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool check(TreeNode *node, int leftVal, int rightVal) { if (node == NULL) return true; return leftVal < node->val && node->val < rightVal && check(node->left, leftVal, node->val) && check(node->right, node->val, rightVal); } bool isValidBST(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function return check(root, INT_MIN, INT_MAX); } };
相關推薦
LeetCode--Validate Binary Search Tree(驗證二叉搜尋樹)C++
題目描述:Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtr
[leetcode]Validate Binary Search Tree (判斷有效二叉搜尋樹 C語言實現)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as
LeetCode: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 subtre
Recover Binary Search Tree(恢復二叉搜尋樹)
題目原型: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solut
LeetCode | 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 no
[LeetCode] Unique Binary Search Trees 獨一無二的二叉搜尋樹
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's.
leetcode----- Validate Binary Search Tree(判斷一棵樹是否是二叉搜尋樹)
1、題目描述 給定一棵二叉樹,判斷這棵樹是否是二叉搜尋樹。 二叉搜尋樹的定義如下: 二叉搜尋樹(Binary Search Tree),又稱二叉排序樹,它或者是一顆空樹,或者具有如下性質的樹: 若它的左子樹不為空,則左子樹上所有節點的值都小於根節點的值 若它的右子樹
LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索樹的眾數)
btn https 標簽 one con pac 發現 log 個數字 Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred
LeetCode 95. Unique Binary Search Trees II(唯一二叉搜尋樹)
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program
[LeetCode] Closest Binary Search Tree Value 最近的二分搜尋樹的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a float
Elven Postman(建二叉搜尋樹)
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something
LeetCode 98. 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 n
LeetCode刷題-98——Validate Binary Search Tree(驗證搜尋二叉樹)
連結:題目:給定一個二叉樹,判斷其是否是一個有效的二叉搜尋樹。一個二叉搜尋樹具有如下特徵:節點的左子樹只包含小於當前節點的數。節點的右子樹只包含大於當前節點的數。所有左子樹和右子樹自身必須也是二叉搜尋樹
【LeetCode】98. Validate Binary Search Tree(C++)
地址:https://leetcode.com/problems/validate-binary-search-tree/ 題目: Given a binary tree, determine if it is a valid binary search tree (BST).
判斷給定二叉樹是否是二叉搜尋樹(LeetCode: Validate Binary Search Tree)
原題:Given a binary tree, determine if it is a valid binary search tree (BST). 方法1:可以根據二叉搜尋樹的規律寫出約束規則。對於二叉搜尋樹的任意結點,其左子樹結點均
[和小菜雞一起刷題(python)] LeetCode 108. 將有序陣列轉換為二叉搜尋樹(Convert Sorted Array to Binary Search Tree)
LeetCode 108. 將有序陣列轉換為二叉搜尋樹(Convert Sorted Array to Binary Search Tree) 原題 思路 程式碼 原題 將一個按照升序排列的有序陣列,轉換為一棵高度平衡二叉搜尋樹。
[LeetCode] 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 onl
98. 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
Validate Binary Search Tree (GOLANG版)
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
98. Validate Binary Search Tree (Tree)
題目連結:https://leetcode.com/problems/validate-binary-search-tree/ 題目描述:判斷BST是否合法。 思路:中序遍歷;對於BST來說,中序遍歷的結果是一個遞增的序列,所以在遞增的過程中判斷即可。 程式碼: clas