标签:二叉树的镜像 队列 遍历 树的子结构 数据结构 依次 res 使用 turn
class Solution { bool isSubtree(TreeNode* pRootA, TreeNode* pRootB) { if (pRootB == NULL) return true; if (pRootA == NULL) return false; if (pRootB->val == pRootA->val) { return isSubtree(pRootA->left, pRootB->left) && isSubtree(pRootA->right, pRootB->right); } else return false; } public: bool HasSubtree(TreeNode* pRootA, TreeNode* pRootB) { if (pRootA == NULL || pRootB == NULL) return false; return isSubtree(pRootA, pRootB) || HasSubtree(pRootA->left, pRootB) || HasSubtree(pRootA->right, pRootB); } };
class Solution { public: //栈的非递归 void Mirror(TreeNode *pRoot) { if (pRoot == NULL)return; stack<TreeNode*> st; TreeNode* p = NULL; st.push(pRoot); while (st.size()) { p = st.top(); st.pop(); swap(p->left, p->right); if (p->left)st.push(p->left); if (p->right)st.push(p->right); } } //队列的非递归 void Mirror(TreeNode *pRoot) { if (pRoot == NULL)return; queue<TreeNode*> qu; TreeNode* p = NULL; qu.push(pRoot); while (qu.size()) { p = qu.front(); qu.pop(); swap(p->left, p->right); if (p->left)qu.push(p->left); if (p->right)qu.push(p->right); } } //递归 void Mirror(TreeNode *pRoot) { if (pRoot == NULL)return; swap(pRoot->left, pRoot->right); Mirror(pRoot->left); Mirror(pRoot->right); } };
class Solution { stack<int> data; stack<int> mindata; public: void push(int value) { data.push(value); if (mindata.size() <= 0){ mindata.push(value); } else if (value < mindata.top()){ mindata.push(value); } else if (value >= mindata.top()){ mindata.push(mindata.top()); } } void pop() { data.pop(); mindata.pop(); } int top() { return mindata.top(); } int min() { return mindata.top(); } };
标签:二叉树的镜像 队列 遍历 树的子结构 数据结构 依次 res 使用 turn
原文地址:http://www.cnblogs.com/yuguangyuan/p/6048081.html