标签:blog 二叉搜索树 item 思路 verify style bst turn div
1 class Solution { 2 public: 3 bool VerifySquenceOfBST1(vector<int> sequence) { 4 if(sequence.size() == 0){ 5 return true; 6 } 7 int i = 0,j = 0; 8 int root = sequence[sequence.size() -1]; 9 vector<int> leftSeq,rightSeq; 10 11 for(i;i < sequence.size() - 1;++i){ 12 if(sequence[i] > root){ 13 break; 14 } 15 leftSeq.push_back(sequence[i]); 16 } 17 18 for(j = i;j < sequence.size() - 1;++j){ 19 if(sequence[j] < root){ 20 return false; 21 } 22 rightSeq.push_back(sequence[j]); 23 } 24 bool left = true; 25 left = VerifySquenceOfBST1(leftSeq); 26 bool right = true; 27 right = VerifySquenceOfBST1(rightSeq); 28 return left && right; 29 } 30 bool VerifySquenceOfBST(vector<int> sequence){ 31 if(sequence.size() == 0){ 32 return false; 33 } 34 return VerifySquenceOfBST1(sequence); 35 36 } 37 };
一定要注意32行和第2行的不同。
标签:blog 二叉搜索树 item 思路 verify style bst turn div
原文地址:http://www.cnblogs.com/dingxiaoqiang/p/7470083.html