解题思路

这是一个判断满二叉树是否为二叉搜索树的问题。关键点:

  1. 二叉搜索树的性质

    • 左子树所有节点值小于根节点
    • 右子树所有节点值大于根节点
    • 左右子树也都是二叉搜索树
  2. 满二叉树的性质

    • 除最后一层外,每层节点都是满的
    • 每个非叶节点都有两个子节点
  3. 数值范围

    • 节点值范围:(0, 65536)
    • 节点数不超过10000

代码

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

class Solution {
public:
    bool check(vector<string>& nums, int index, long long min_val, long long max_val) {
        if (index >= nums.size()) {
            return true;
        }
        
        int val = stoi(nums[index]);
        if (val <= 0 || val >= 65536) {
            return false;
        }
        
        if (val <= min_val || val >= max_val) {
            return false;
        }
        
        int left = 2 * index + 1;
        int right = 2 * index + 2;
        
        if (left < nums.size()) {
            return check(nums, left, min_val, val) && check(nums, right, val, max_val);
        }
        
        return true;
    }
};

int main() {
    string line;
    while (getline(cin, line)) {
        if (line == "None") {
            cout << "True" << endl;
            continue;
        }
        
        vector<string> nums;
        stringstream ss(line);
        string item;
        while (getline(ss, item, ',')) {
            nums.push_back(item);
        }
        
        Solution sol;
        try {
            cout << (sol.check(nums, 0, 0, 65536) ? "True" : "False") << endl;
        } catch (...) {
            cout << "False" << endl;
        }
    }
    return 0;
}
import java.util.*;

public class Main {
    public static boolean check(String[] nums, int index, long min_val, long max_val) {
        if (index >= nums.length) {
            return true;
        }
        
        try {
            long val = Long.parseLong(nums[index]);
            if (val <= 0 || val >= 65536) {
                return false;
            }
            
            if (val <= min_val || val >= max_val) {
                return false;
            }
            
            int left = 2 * index + 1;
            int right = 2 * index + 2;
            
            if (left < nums.length) {
                return check(nums, left, min_val, val) && check(nums, right, val, max_val);
            }
            
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String line = sc.nextLine().trim();
            if (line.equals("None")) {
                System.out.println("True");
                continue;
            }
            
            String[] nums = line.split(",");
            System.out.println(check(nums, 0, 0L, 65536L) ? "True" : "False");
        }
    }
}
class Solution:
    def isValidBST(self, nums):
        # 空树情况
        if not nums or nums[0] == "None":
            return True
            
        # 转换输入为整数列表
        nums = [int(x) for x in nums]
        
        def check(index, min_val, max_val):
            if index >= len(nums):
                return True
                
            val = nums[index]
            if val <= 0 or val >= 65536:
                return False
                
            if val <= min_val or val >= max_val:
                return False
            
            # 检查左右子树
            left = 2 * index + 1
            right = 2 * index + 2
            
            # 满二叉树要么都有子节点,要么都没有
            if left < len(nums):
                if not check(left, min_val, val):
                    return False
                if not check(right, val, max_val):
                    return False
            
            return True
        
        return check(0, 0, 65536)

# 处理输入
while True:
    try:
        line = input().strip()
        if line == "None":
            print("True")
            continue
            
        nums = line.split(",")
        solution = Solution()
        print(str(solution.isValidBST(nums)))
    except:
        break

算法及复杂度

  • 算法:递归判断 + 区间检查
  • 时间复杂度:,其中 为节点数量
  • 空间复杂度:,其中 为树的高度