二叉树结构
//Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
1、前序 beat 100%
144. Binary Tree Preorder Traversal
弹出打印,先压右再压左, 压人顺序:中右左 弹出打印顺序:中左右
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL ) return res;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()){
root=s.top();
s.pop();
res.push_back(root->val);
if(root->right){
s.push(root->right);
}
if(root->left){
s.push(root->left);
}
}
return res;
}
};
2、中序 beat100%
94. Binary Tree Inorder Traversal
判断 不为空 一直压左节点,为空 弹出打印 指向右节点
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(root ==NULL) return res;
stack<TreeNode* > s;
while(root || !s.empty()){
if(root){
s.push(root);
root=root->left;
}
else{
root=s.top();
s.pop();
res.push_back(root->val);
root=root->right;
}
}
return res;
}
};
3、后序 beat100%
145. Binary Tree Postorder Traversal
方法一:两个栈 把所有要打印的都push到s2中,统一打印
先序:压栈 :中右左 打 印:中左右
后序:压栈s1:中左右 压栈s2:中右左 打印:左右中
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL) return res;
stack<TreeNode*> s1;
stack<TreeNode*> s2;
s1.push(root);
while(!s1.empty()){
root=s1.top();
s1.pop();
s2.push(root);
if(root->left){
s1.push(root->left);
}
if(root->right){
s1.push(root->right);
}
}
while(!s2.empty()){
root=s2.top();
s2.pop();
res.push_back(root->val);
}
return res;
}
};
方法二:判断栈顶c与root关系
1、如果c有左孩子且root不是c的孩子 把c做孩子压栈
2、如果c有右孩子且root不是c的右孩子,把c右孩子压栈
3、其他情况打印c的值,令root=c
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL) return res;
stack<TreeNode*> s1;
s1.push(root);
TreeNode * c=NULL;
while(!s1.empty()){
c=s1.top();
if( c->left && root!=c->left && root!=c->right ){
s1.push(c->left);
}
else if(c->right && root !=c->right){
s1.push(c->right);
}
else {
res.push_back(c->val);
s1.pop();
root=c;
}
}
return res;
}
};
4、层序 100%
5、合并两个二叉树
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if(!t1) return t2;
if(!t2) return t1;
TreeNode* node=new TreeNode(t1->val+t2->val);
node->left=mergeTrees(t1->left,t2->left);
node->right=mergeTrees(t1->right,t2->right);
return node;
}
};
6、 二叉搜索树搜索
700. Search in a Binary Search Tree
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==NULL || root->val==val) return root;
return val>root->val?searchBST(root->right,val):searchBST(root->left,val);
}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==NULL || root->val==val) return root;
TreeNode* cur = root;
while(cur->val!=val){
if(cur->val<val){
cur=cur->right;
}
else
cur=cur->left;
if(cur==NULL ) return NULL;
}
return cur;
}
};
7、N叉树后序遍历
class Solution {
public:
vector<int> postorder(Node* root) {
vector<int> res;
if (!root) {
return res;
}
stack<Node*> s1;
stack<Node*> s2;
s1.push(root);
Node* node;
while (!s1.empty()) {
node = s1.top();
s1.pop();
s2.push(node);
if (node->children.empty()) {
continue;
}
for (int i = 0; i < node->children.size(); ++i) {
s1.push(node->children[i]);
}
}
while(!s2.empty()){
node=s2.top();
s2.pop();
res.push_back(node->val);
}
return res;
}
};
8、判断两棵树叶子相同
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
stack<TreeNode*> s1 , s2;
s1.push(root1); s2.push(root2);
while (!s1.empty() && !s2.empty())
if (dfs(s1) != dfs(s2)) return false;
return s1.empty() && s2.empty();
}
int dfs(stack<TreeNode*>& s) {
while (true) {
TreeNode* node = s.top(); s.pop();
if (node->right) s.push(node->right);
if (node->left) s.push(node->left);
if (!node->left && !node->right) return node->val;
}
}
};
9、n叉树最大深度
class Solution {
public:
int maxDepth(Node* root) {
if(root==NULL) return 0;
vector<Node* > child;
if(root){
child=root->children;
}
int maxval=1;
for(int i=0;i<child.size();i++){
if(child[i]){
int depth=1+maxDepth(child[i]);
maxval=max(maxval,depth);
}
}
return maxval;
}
};
class Solution {
public:
int maxDepth(Node* root) {
int res=0;
if(root == NULL) return 0;
dfs(root, 0, res);
return res + 1;
}
void dfs(Node* root, int depth, int& res) {
res = max(res, depth);
if(root == NULL) return ;
for(int i = 0;i < root->children.size(); i++) {
dfs(root->children[i], depth + 1, res);
}
}
};
10、 n叉树前序
589. N-ary Tree Preorder Traversal
class Solution {
public:
vector<int> preorder(Node* root) {
vector<int> res;
stack<Node*> s;
s.push(root);
while(!s.empty()){
root=s.top();
s.pop();
if(root==NULL) continue;
for(int i=root->children.size()-1;i>=0;i--){
s.push(root->children[i]);
}
res.push_back(root->val);
}
return res;
}
};
11、二叉搜索树修剪
669. Trim a Binary Search Tree
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
int flag=1;
TreeNode* res;
dfs(root,L,R,flag,res);
return res;
}
TreeNode* dfs(TreeNode* root, int L, int R,int flag,TreeNode*& res){
if(!root) return NULL;
if(root->val<L){
return dfs(root->right,L,R,flag,res);
}
else if(root->val>R){
return dfs(root->left,L,R,flag,res);
}
else{
root->left=dfs(root->left,L,R,2,res);
root->right=dfs(root->right,L,R,2,res);
if(flag==1) res=root;
return root;
}
}
};
12、 二叉搜索树重新排序
897. Increasing Order Search Tree
/**
* 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:
TreeNode* increasingBST(TreeNode* root) {
if(!root) return root;
stack<TreeNode*> s;
while(root->left){
s.push(root);
root=root->left;
}
TreeNode* node =root;
TreeNode* tail=NULL;
while(node || !s.empty()){
if(node){
s.push(node);
node=node->left;
}
else{
node=s.top();
s.pop();
if(tail){
tail->right=node;
node->left=NULL;
}
tail=node;
node=node->right;
}
}
return root;
}
};
13、 二叉树高度
104. Maximum Depth of Binary Tree
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0;
int l=maxDepth(root->left);
int r=maxDepth(root->right);
return max(l,r)+1;
}
};
14、二叉树每层平均值
637. Average of Levels in Binary Tree
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
q.push(NULL);
double sum=0;
int cnt=0;
while(!q.empty()){
root=q.front();
q.pop();
if(!root){
res.push_back(sum/cnt);
if(!q.empty()){
q.push(NULL);
}
sum=0;
cnt=0;
continue;
}
cnt++;
sum+=root->val;
if(root->left){
q.push(root->left);
}
if(root->right){
q.push(root->right);
}
}
return res;
}
};
15、二叉树翻转
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
TreeNode* invert=new TreeNode(root->val);
invert->left=invertTree(root->right);
invert->right=invertTree(root->left);
return invert;
}
};
16、 N叉树层序遍历
429. N-ary Tree Level Order Traversal
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if(!root) return res;
queue<Node*> q;
q.push(root);
while(!q.empty()){
int len=q.size();
vector<int> tmp;
for (int i = 0; i < len; i++){
root=q.front();
q.pop();
tmp.push_back(root->val);
vector<Node*> child= root->children;
for(int i=0;i<child.size();i++){
q.push(child[i]);
}
}
res.push_back(tmp);
}
return res;
}
};
17、二叉树序列化
class Solution {
public:
string tree2str(TreeNode* t) {
string s="";
if(!t) return s;
build_string(t,s);
return s;
}
void build_string(TreeNode* t ,string& s){
if(!t){
s+="()";
return;
}
if(t){
s+=to_string(t->val);
}
if(t->left){
s+="(";
build_string(t->left,s);
s+=")";
}
else if(t->right){
s+="()";
}
if(t->right){
s+="(";
build_string(t->right,s);
s+=")";
}
}
};
18、二叉搜索树转换
538. Convert BST to Greater Tree
class Solution {
public:
vector<TreeNode *> num;
TreeNode* convertBST(TreeNode* root) {
tree2vec(root);
if(num.size()<=1) return root;
for(int i=num.size()-2;i>=0;i--){
num[i]->val+=num[i+1]->val;
}
return root;
}
private:
void tree2vec(TreeNode *root) {
if (!root) return;
tree2vec(root->left);
num.push_back(root);
tree2vec(root->right);
}
};
1、平衡二叉树
beat 100% 包含剪枝,底层不平衡,就一路返回,且当左子树高度为-1时,就没必要去求右子树的高度
class Solution {
public:
bool isBalanced(TreeNode* root) {
return getdepth(root)!=-1;
}
private:
int getdepth(TreeNode* root){
if(root==NULL) return 0;
int left=getdepth(root->left);
if(left==-1) return -1;
int right=getdepth(root->right);
if(right==-1) return -1;
return abs(left-right)>1? -1: max(left,right)+1;
}
};
2、判断二叉搜索树
方法一:
class Solution {
public:
bool isValidBST(TreeNode* root) {
return isValidBST(root, NULL, NULL);
}
bool isValidBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode) {
if(!root) return true;
if(minNode && root->val <= minNode->val || maxNode && root->val >= maxNode->val)
return false;
return isValidBST(root->left, minNode, root) && isValidBST(root->right, root, maxNode);
}
};
方法二:in-order
class Solution {
public:
bool flag;
bool isValidBST(TreeNode* root) {
flag=true;
TreeNode* pre=NULL;
in_order(root,pre);
return flag;
}
void in_order(TreeNode* root,TreeNode*& pre){
if(root==NULL) return ;
in_order(root->left,pre);
if(pre!=NULL&&pre->val>=root->val) flag=false;
pre=root;
in_order(root->right,pre);
}
};