###不给自己任何借口
今日题目:
1、判断是否BST
2、二叉树的后序遍历
今日摘录:
人们宁愿去关心一个蹩脚电影演员的吃喝拉撒和鸡毛蒜皮,而不愿了解一个普通人波涛汹涌的内心世界……
——路遥《平凡的世界》
98. Validate Binary Search Tree | Difficulty: Medium
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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
tag:树|DFS
题意:判断一棵二叉树是否合法
思路:
1、迭代思想,对树进行中序遍历,然后看看遍历元素是否顺序。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(!root) return true;
vector<int>res;
stack<TreeNode*>nodes;
TreeNode*cur = root;
while(cur || !nodes.empty())
{
while(cur)
{
nodes.push(cur);
cur = cur->left;
}
cur = nodes.top();
res.push_back(cur->val);
nodes.pop();
cur = cur->right;
}
for(int i=0;i<res.size()-1;i++)
{
if(res[i]>=res[i+1]) return false;
}
return true;
}
};
结果:16ms
2、递归版本
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValid(TreeNode*root,long min,long max)
{
if(!root) return true;
if(root->val <= min || root->val >= max) return false;
return isValid(root->left,min,root->val) && isValid(root->right,root->val,max);
}
bool isValidBST(TreeNode* root) {
if(!root) return true;
return isValid(root,LONG_MIN,LONG_MAX);
}
};
结果:12ms
145. Binary Tree Postorder Traversal | Difficulty: Hard
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].
tag:树|栈
题意:后序遍历二叉树
思路:
1、递归版本
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
if(!root) return res;
postorder(root,res);
return res;
}
void postorder(TreeNode* root,vector<int>&res)
{
if(!root) return;
postorder(root->left,res);
postorder(root->right,res);
res.push_back(root->val);
}
};
结果:0ms
2、迭代版本,其实就是做先序遍历,然后再反过来。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
if(!root) return res;
stack<TreeNode*> nodes;
nodes.push(root);
while(!nodes.empty())
{
TreeNode* cur = nodes.top();
nodes.pop();
res.push_back(cur->val);
if(cur->left) nodes.push(cur->left);
if(cur->right) nodes.push(cur->right);
}
reverse(res.begin(),res.end());
return res;
}
};
结果:4ms