Leetcode 98. Validate Binary Search Tree 驗證二叉搜尋樹 解題報告
1 解題思想
這題我的做法,直接就是先序遍歷,遍歷過程中,檢查是否出錯了,程式碼很簡單。
注意用於上一個值追蹤的last,一開始必須要比Int的最小值小才行,所以用了long
2 原題
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.
3 AC解
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
long last = Long.MIN_VALUE;
//採用左中右遍歷後檢查是否遞增
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
if ( !isValidBST(root.left) ) return false;
if (root.val <= last) return false;
last = root.val;
return isValidBST(root.right) ;
}
}
相關推薦
Leetcode 98. Validate Binary Search Tree 驗證二叉搜尋樹 解題報告
1 解題思想 這題我的做法,直接就是先序遍歷,遍歷過程中,檢查是否出錯了,程式碼很簡單。 注意用於上一個值追蹤的last,一開始必須要比Int的最小值小才行,所以用了long 2 原題 Give
LeetCode 98.Validate Binary Search Tree (驗證二叉搜尋樹)
題目描述: 給定一個二叉樹,判斷其是否是一個有效的二叉搜尋樹。 假設一個二叉搜尋樹具有如下特徵: 節點的左子樹只包含小於當前節點的數。 節點的右子樹只包含大於當前節點的數。 所有左子樹和右子樹自身必須也是二叉搜尋樹。 示例 1: 輸入: 2 / \
Leetcode 98 Validate Binary Search Tree 驗證二叉查詢樹
題目描述 Given a binary tree, determine if it is a valid binary search tree (BST). 給出一個二叉樹,判斷其是否是合法的二叉查詢樹。 解題思路 首先,我們來看二叉查詢樹的
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] 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
【LeetCode】Validate Binary Search Tree 驗證二叉查詢樹
驗證二叉查詢樹 給定一個二叉樹,判斷它是否是合法的二叉查詢樹(BST) 一棵BST定義為: 節點的左子樹中的值要嚴格小於該節點的值。 節點的右子樹中的值要嚴格大於該節點的值。 左右子樹也必須是二叉查詢樹。 一個節點的樹也是二叉查詢樹。 樣例
Validate Binary Search Tree 驗證二叉搜尋樹
給定一個二叉樹,判斷其是否是一個有效的二叉搜尋樹。一個二叉搜尋樹具有如下特徵:節點的左子樹只包含小於當前節點的數。節點的右子樹只包含大於當前節點的數。所有左子樹和右子樹自身必須也是二叉搜尋樹。示例 1:輸入: 2 / \ 1 3 輸出: true 示例
Leetcode98. Validate Binary Search Tree驗證二叉搜尋樹
給定一個二叉樹,判斷其是否是一個有效的二叉搜尋樹。 假設一個二叉搜尋樹具有如下特徵: 節點的左子樹只包含小於當前節點的數。 節點的右子樹只包含大於當前節點的數。 所有左子樹和右子樹自身必須也是二叉搜尋樹。 示例 1: 輸入: 2 / \ 1 3 輸出: true
[LeetCode] Verify Preorder Sequence in Binary Search Tree 驗證二叉搜尋樹的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the seque
lintcode validate-binary-search-tree 驗證二叉查詢樹
問題描述 筆記 程式碼1是用到中序遍歷,要求中序遍歷是嚴格的增序。用到了輔助空間。 程式碼2是leetcode上面的解法,用到了prev指標記錄前一個節點,省下了輔助空間,而且要注意prev傳
[leetcode]270. Closest Binary Search Tree Value二叉搜尋樹中找target的最接近值
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
[leetcode]98. Validate Binary Search Tree驗證BST
題意 define defined sum etc validate int 一個 nta Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d
[leetcode]173. Binary Search Tree Iterator 二叉搜尋樹迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return
LeetCode 173.Binary Search Tree Iterator (二叉搜尋樹迭代器)
題目描述: 實現一個二叉搜尋樹迭代器。你將使用二叉搜尋樹的根節點初始化迭代器。 呼叫 next() 將返回二叉搜尋樹中的下一個最小的數。 注意: next() 和hasNext() 操作的時間複雜度是O(1),並使用 O(h)&
[LeetCode] 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 solution using O(n) space is
[LeetCode] Binary Search Tree Iterator 二叉搜尋樹迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next
pat-A1043:Is it a Binary Search Tree(二叉搜尋樹和及其映象樹的遍歷)
目錄 題目解釋: 解題思路: ac程式碼: 題目地址:https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 題目解釋: 給出一個二叉樹的序列,判斷它是否是“二叉搜尋樹
LeetCode 95. Unique Binary Search Trees II (二叉搜尋樹計數,卡特蘭數)
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n. Example: Input: 3 Output: [ [1,nul
LeetCode 96. Unique Binary Search Trees (獨立二叉搜尋樹)
原題 Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n? Example: Input: 3 Output: 5 Explanation: Given
資料結構----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 sol