标签:树的镜像 continue alt vector 返回 subject bool image 实现
/* struct TreeLinkNode { int val; struct TreeLinkNode *left; struct TreeLinkNode *right; struct TreeLinkNode *next; TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) { } }; */ class Solution { TreeLinkNode* getNode(TreeLinkNode* pNode){ TreeLinkNode*pre = NULL; while (pNode){ pre = pNode; pNode = pNode->left; } return pre; } public: TreeLinkNode* GetNext(TreeLinkNode* pNode) { TreeLinkNode*tmp = pNode->next; if (tmp){//如果有父节点 if (tmp->left == pNode&&pNode->right == NULL){//如果是左节点,就返回他的爸爸 return tmp; } else if (tmp->left == pNode&&pNode->right != NULL){ return getNode(pNode->right); } if (tmp->right == pNode){//如果是右节点,就返回他的右儿子 if (pNode->right){ return getNode(pNode->right); } else if (tmp->next->left == tmp){ return tmp->next; } else if (tmp->next->right == tmp){ return NULL; } } } else{//根节点 return getNode(pNode->right); } return NULL; } };
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: bool isSymmetrical(TreeNode* pRoot) { return isSymmetrical(pRoot,pRoot); } bool isSymmetrical(TreeNode* pRoot1,TreeNode* pRoot2) { if(pRoot1==NULL&&pRoot2==NULL) return true; if(pRoot1==NULL || pRoot2==NULL) return false; if(pRoot1->val!=pRoot2->val) return false; return isSymmetrical(pRoot1->left,pRoot2->right) && isSymmetrical(pRoot1->right,pRoot2->left); } };
不使用reverse!!! 借助两个stack,分别为s1和s2。
1. 首先将根节点压入栈s1。
2. 将s1依次出栈,保存每个节点值,并依次将每个节点的左右节点压入栈s2
3. 将s2依次出栈,保存每个节点值,并依次将每个节点的右左节点压入本s1<注:这里是先压右子
节点再压左子节点>
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int> > result; if (!pRoot) { return result; } stack<TreeNode*> s1; stack<TreeNode*> s2; vector<int> temp; s1.push(pRoot); while (true) { while (!s1.empty()) { TreeNode* ptr = s1.top(); s1.pop(); if (!ptr) { continue; } s2.push(ptr->left); s2.push(ptr->right); temp.push_back(ptr->val); } if (temp.empty()) { break; } result.push_back(temp); temp.clear(); while (!s2.empty()) { TreeNode* ptr = s2.top(); s2.pop(); if (!ptr) { continue; } s1.push(ptr->right); s1.push(ptr->left); temp.push_back(ptr->val); } if (temp.empty()) { break; } result.push_back(temp); temp.clear(); } return result; } };
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { queue<TreeNode*>qu1; queue<TreeNode*>qu2; vector<int> a; vector<vector<int> >as; if(!pRoot) return as; qu1.push(pRoot); while(1){ while(!qu1.empty()){ TreeNode* tmp=qu1.front(); qu1.pop(); if(!tmp)continue; a.push_back(tmp->val); qu2.push(tmp->left); qu2.push(tmp->right); } if(a.empty()){break;} as.push_back(a); a.clear(); while(!qu2.empty()){ TreeNode* tmp=qu2.front(); qu2.pop(); if(!tmp)continue; a.push_back(tmp->val); qu1.push(tmp->left); qu1.push(tmp->right); } if(a.empty()){break;} as.push_back(a); a.clear(); } return as; } };
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { private: TreeNode* decode(char *&str) { if(*str==‘#‘){ str++; return NULL; } int num = 0; while(*str != ‘,‘) num = num*10 + (*(str++)-‘0‘); str++; TreeNode *root = new TreeNode(num); root->left = decode(str); root->right = decode(str); return root; } public: char* Serialize(TreeNode *root) { if(!root) return "#"; string r = to_string(root->val); r.push_back(‘,‘); char *left = Serialize(root->left); char *right = Serialize(root->right); char *ret = new char[strlen(left) + strlen(right) + r.size()]; strcpy(ret, r.c_str()); strcat(ret, left); strcat(ret, right); return ret; } TreeNode* Deserialize(char *str) { return decode(str); } };
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: TreeNode* KthNode(TreeNode* pRoot, int k) { vector<TreeNode*>a; stack<TreeNode*>stack; TreeNode* p = pRoot; while(p || !stack.empty()){ if(p!=NULL){ stack.push(p); p = p->left; }else{ p = stack.top(); a.push_back(p); stack.pop(); p = p ->right; } } if(k > a.size()){ return NULL; }else if(k == 0){ return NULL; }else{ return a[k-1]; } } };
class Solution { vector<int>a; public: void Insert(int num) { a.push_back(num); } double GetMedian() { vector<int>b=a; sort(b.begin(), b.end()); if(b.size()%2 == 1){ return b[b.size()/2]; }else{ return (b[b.size()/2]+b[b.size()/2-1])/2.0; } } };
class Solution { int getVal(vector<int>v){ int max = v[0]; for (int i = 0; i<v.size(); i++){ if (max < v[i]){ max = v[i]; } } return max; } public: vector<int> maxInWindows(const vector<int>& num, unsigned int size) { vector<int>a; vector<int>tmp; if (num.size()<size||size == 0) return a; for (int i = 0; i<num.size() - size+1; i++){ for (int j = 0; j<size; j++){ tmp.push_back(num[j + i]); } a.push_back(getVal(tmp)); tmp.clear(); } return a; } };
标签:树的镜像 continue alt vector 返回 subject bool image 实现
原文地址:http://www.cnblogs.com/yuguangyuan/p/6085283.html