Leetcode题解(30)

98. Validate Binary Search Tree

题目

分析:BST按照中序遍历之后所得到的序列是一个递增序列,因此可以按照这个思路,先中序遍历,保存好遍历的结果,然后在遍历一遍这个序列。判断其是否递增

代码如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isValidBST(TreeNode* root) {
13         if(NULL == root)
14             return true;
15         vector<int> res;
16         inOrderTraversal(root,res);
17         for(int i=1;i<res.size();i++)
18         {
19             if(res[i]<=res[i-1])
20                 return false;
21         }
22         return true;
23     }
24     void inOrderTraversal(TreeNode* root,vector<int> &res)
25     {
26         if(NULL==root)
27             return;
28         inOrderTraversal(root->left,res);
29         res.push_back(root->val);
30         inOrderTraversal(root->right,res);
31
32     }
33 };

上面的实现中,使用了一个vector,需要额外的O(n)空间,但是仔细一想,当在保存某一个之值时,只需要判断在它之前的那个数是否小于它,如果小于,则继续遍历,如果不小于,则可以返回false。

代码如下,每次递归调用的时候,都将当前已经遍历的最大数字保存在key中即可

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isValidBST(TreeNode* root) {
13         if(NULL == root)
14             return true;
15
16         long long key = -2147483649;
17         return inOrderTraversal(root,key);
18
19     }
20     bool inOrderTraversal(TreeNode* root,long long &key)
21     {
22         if(NULL==root)
23             return true;
24         bool flag = inOrderTraversal(root->left,key);
25         if(flag == false)
26             return false;
27         if(root->val <= key)
28         {
29             return false;
30         }
31         key = root->val;
32         flag = inOrderTraversal(root->right,key);
33         if(flag == false)
34             return false;
35         return true;
36
37
38
39
40     }
41 };
时间: 2024-08-11 05:32:33

Leetcode题解(30)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative