bool isValidBST(TreeNode* root) {
return dfs(root,INT_MIN,INT_MAX);
}
bool dfs(TreeNode*root,long long MINV,long long MAXV)
{
if(!root) return true;
if(root->val<MINV || root->val > MAXV) return false;
return dfs(root->left,MINV,root->val-1ll) && dfs(root->right,root->val+1ll,MAXV);
}
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> stk;
vector<int>ans;
auto p=root;
while(p || stk.size())
{
while(p)
{
stk.push(p);
p=p->left;
}
p=stk.top();
stk.pop();
ans.push_back(p->val);
p=p->right;
}
return ans;
}
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return dfs(root->left,root->right);
}
bool dfs(TreeNode* p,TreeNode* q)
{
if(!p || !q) return !q && !p;
return p->val==q->val && dfs(p->left,q->right) && dfs(p->right,q->left);
}
bool isSymmetric(TreeNode* root) {
if(!root) return true;
stack<TreeNode*> right,left;
auto p=root->left,q=root->right;
while(p || q || right.size() || left.size())
{
while(p && q)
{
left.push(p);
p=p->left;
right.push(q);
q=q->right;
}
if(p || q) return false;
p=left.top(),left.pop();
q=right.top(), right.pop();
if(p->val!=q->val) return false;
p=p->right;q=q->left;
}
return true;
}
unordered_map<int,int>pos;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int n=preorder.size();
for(int i=0;i<n;i++) pos[inorder[i]]=i;
return dfs(preorder,inorder,0,n-1,0,n-1);
}
TreeNode* dfs(vector<int>& preorder, vector<int>& inorder,int pl,int pr,int il,int lr)
{
if(pl>pr) return NULL;
int val=preorder[pl];
int k=pos[val];
int len=k-il;
auto root=new TreeNode(val);
root->left=dfs(preorder,inorder,pl+1,pl+len,il,k-1);
root->right=dfs(preorder,inorder,pl+len+1,pr,k+1,lr);
return root;
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (!root) return ans;
queue<TreeNode*> temp;
vector<int> each_ans;
temp.push(root);
while(!temp.empty())
{
int n=temp.size();
for(int i=0;i<n;i++)
{
auto p=temp.front();
temp.pop();
each_ans.push_back(p->val);
if(p->left) temp.push(p->left);
if(p->right) temp.push(p->right);
}
ans.push_back(each_ans);
each_ans.clear();
}
return ans;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || root==p || root==q) return root;
auto left=lowestCommonAncestor(root->left,p,q);
auto right=lowestCommonAncestor(root->right,p,q);
if(!left) return right;
if(!right) return left;
return root;
}
int ans=0;
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode*root)
{
if(!root) return 0;
int left=dfs(root->left);
int right=dfs(root->right);
ans=max(ans,left+right);
return max(left+1,right+1);
}
int ans=INT_MIN;
int maxPathSum(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode* root)
{
if(!root) return 0;
int left=dfs(root->left);
int right=dfs(root->right);
ans=max(ans,root->val+right+left);
return max(0,max(0,root->val+max(left,right)));
}
class BSTIterator {
public:
//用迭代的方式中序遍历整个二叉树
stack<TreeNode*> stk;
BSTIterator(TreeNode* root) {
while(root)
{
stk.push(root);
root=root->left;
}
}
/** @return the next smallest number */
int next() {
auto p=stk.top();
stk.pop();
int res=p->val;
p=p->right;
while(p)
{
stk.push(p);
p=p->left;
}
return res;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !stk.empty();
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs1(root,res);
return res;
}
void dfs1(TreeNode* root,string& res)
{
if(!root)
{
res+="#,";
return ;
}
res+=to_string(root->val)+',';
dfs1(root->left,res);
dfs1(root->right,res);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int u=0;
return dfs2(data,u);
}
TreeNode* dfs2(string &data,int &u)
{
if(data[u]=='#')
{
u+=2;
return NULL;
}
bool is_minus=false;
if(data[u]=='-')
{
is_minus=true;
u++;
}
int t=0;
while(data[u]!=',')
{
t = t*10+ data[u]- '0';
u++;
}
u++;
if(is_minus) t=-t;
auto root=new TreeNode(t);
root->left=dfs2(data,u);
root->right=dfs2(data,u);
return root;
}
};