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.
給予一個二元樹,判斷它是否為合法的二元搜索樹(BST)。
一個BST的定義為以下:
- 左邊的子樹只會包含比你小的key的節點。
- 右邊的子樹只會包含比你大的key的節點。
- 左子樹和右子樹也都是二元搜索樹。
NULL
(空節點)為止都沒有錯誤就回傳true
。LeetCode
C++