Skip to content

Commit 3e0d738

Browse files
committed
study 二叉平衡树的特点。
1 parent 091c9da commit 3e0d738

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 你要把最实质的规律弄明白,就是不是简单的判断一个节点上的左右子节点的与父节点之间的大小关系,而是当左部分子树中的
2+
右小子树是需要满足小于上一层节点的父节点,而右部分子树中的左小子树是需要满足大于上一层节点的父母的!!所以你的递归终止
3+
条件是需要斟酌的!
4+
5+
下面的代码是最优代码
6+
class Solution {
7+
bool dfs(TreeNode *node,int lower,int upper)
8+
{
9+
if(node == NULL)
10+
return true;
11+
else
12+
return node->val > lower && node->val < upper && dfs(node->left,lower,node->val) && dfs(node->right,node->val,upper);
13+
14+
}
15+
public:
16+
bool isValidBST(TreeNode *root) {
17+
return dfs(root,INT_MIN,INT_MAX);
18+
}
19+
};

0 commit comments

Comments
 (0)