# 98. Validate Binary Search Tree 題目:<https://leetcode.com/problems/validate-binary-search-tree/> 解法:DFS,設定最小邊界 minBound 和最大邊界 maxBound,判斷目前節點的值 val,如果 minBound < val < maxBound,則左子樹所有節點的值必須介於(minBound, val)之間,右子樹所有節點的值必須介於(val, maxBound)之間。 Python3: ``` python 3 class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None def listToTree(arr): nodes = [TreeNode(arr[0])] i = 0 j = 1 length = len(arr) while j < length: if arr[j] is not None: tmp = TreeNode(arr[j]) nodes[i].left = tmp nodes.append(tmp) j += 1 if j >= length: break if arr[j] is not None: tmp = TreeNode(arr[j]) nodes[i].right = tmp nodes.append(tmp) j += 1 i += 1 return nodes[0] def treeToList(root): from collections import deque nodes = deque() nodes.append(root) arr = [] arr.append(root.val) while len(nodes): node = nodes.popleft() if node.left: nodes.append(node.left) arr.append(node.left.val) else: arr.append(None) if node.right: nodes.append(node.right) arr.append(node.right.val) else: arr.append(None) return arr class Solution: def isValidBST(self, root: TreeNode) -> bool: def isValid(root, minBound, maxBound): if not root: return True if root.val > minBound and root.val < maxBound: return isValid(root.left, minBound, root.val) \ and isValid(root.right, root.val, maxBound) else: return False return isValid(root, -2**31-1, 2**31) if __name__ == '__main__': arr = [5,4,6,None,None,3,7] root = listToTree(arr) print(treeToList(root)) ans = Solution().isValidBST(root) print(ans) ``` C: ``` c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; struct TreeNode* strToTree(char *str) { char *token = strtok(str, ","); struct TreeNode **nodes = (struct TreeNode**)malloc(sizeof(struct TreeNode*)); nodes[0] = (struct TreeNode*)malloc(sizeof(struct TreeNode)); nodes[0]->val = atoi(token); nodes[0]->left = NULL; nodes[0]->right = NULL; int i = 0, len = 1; token = strtok(NULL, ","); while (token != NULL) { if (strcmp(token, "null") != 0) { len++; nodes = (struct TreeNode**)realloc(nodes, sizeof(struct TreeNode*) * len); nodes[len - 1] = (struct TreeNode*)malloc(sizeof(struct TreeNode)); nodes[len - 1]->val = atoi(token); nodes[len - 1]->left = NULL; nodes[len - 1]->right = NULL; nodes[i]->left = nodes[len - 1]; } token = strtok(NULL, ","); if (token == NULL) break; if (strcmp(token, "null") != 0) { len++; nodes = (struct TreeNode**)realloc(nodes, sizeof(struct TreeNode*) * len); nodes[len - 1] = (struct TreeNode*)malloc(sizeof(struct TreeNode)); nodes[len - 1]->val = atoi(token); nodes[len - 1]->left = NULL; nodes[len - 1]->right = NULL; nodes[i]->right = nodes[len - 1]; } token = strtok(NULL, ","); i++; } return nodes[0]; } void printLevelOrderWithNull(struct TreeNode* root) { struct TreeNode * bfs[10000] = {}; bfs[0] = root; int i = 0, len = 1; printf("%d ", root->val); while (i < len) { if (bfs[i]->left != NULL) { bfs[len++] = bfs[i]->left; printf("%d ", bfs[i]->left->val); } else printf("null "); if (bfs[i]->right != NULL) { bfs[len++] = bfs[i]->right; printf("%d ", bfs[i]->right->val); } else printf("null "); i++; } printf("\n"); } bool isValidBST(struct TreeNode* root){ bool isValid(struct TreeNode* root, long long int minBound, long long int maxBound) { if (root == NULL) return true; if (root->val > minBound && root->val < maxBound) return isValid(root->left, minBound, root->val) && isValid(root->right, root->val, maxBound); else return false; } return isValid(root, LLONG_MIN, LLONG_MAX); } int main() { char input[] = "5,4,6,null,null,3,7"; struct TreeNode* root = strToTree(input); printLevelOrderWithNull(root); printf("\n"); bool ans = isValidBST(root); printf("%s\n", ans ? "True": "False"); return 0; } ``` ###### tags: `leetcode` `binary tree` `binary search tree` `depth-first search`