Symmetric tree

Given a binary tree, check whether it is a mirror of itself (ie, shortric around its center ).

For example, this binary tree is unsupported Ric:

1 / 2 2 / \ / 3 4 4 3

But the following is not:

1 / 2 2 \ 3 3

Recursive solution: Due to symmetry, each time the left subtree of a node is compared with the left subtree of its brother node, and then recursively processed.

struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:bool isSymmetricRecursive(TreeNode* root1,TreeNode* root2){if(root1 == NULL && root2 == NULL)return true;if(root1 == NULL || root2 == NULL)return false;return (root1->val == root2->val) && isSymmetricRecursive(root1->left,root2->right) && isSymmetricRecursive(root1->right,root2->left);} bool isSymmetric(TreeNode *root) { if(root == NULL)return true;return isSymmetricRecursive(root->left,root->right); }};

Iteration Method: similar to recursion, two queues are used to process the child of the Left subtree In the first queue, and the child of the right subtree In the second queue, judge the first element of each team, which is several times faster than Recursion

Struct treenode {int val; treenode * left; treenode * right; treenode (int x): Val (x), left (null), right (null ){}}; class solution {public: bool issymmetric (treenode * root) {// you can determine whether the image tree is different from the image tree if (! Root |! Root-> left &&! Root-> right) return true; queue <treenode *> leftqueue, rightqueue; leftqueue. push (root-> left); rightqueue. push (root-> right); While (leftqueue. size ()> 0 & rightqueue. size ()> 0) {treenode * pnode1 = leftqueue. front (); leftqueue. pop (); treenode * pnode2 = rightqueue. front (); rightqueue. pop (); If (! Pnode1 &&! Pnode2) continue; // All are empty if (! Pnode1 |! Pnode2) return false; // only one empty if (pnode1-> Val! = Pnode2-> Val) return false; // No leftqueue is empty. push (pnode1-> left), leftqueue. push (pnode1-> right); // The first queue is advanced to the left subtree rightqueue. push (pnode2-> right), rightqueue. push (pnode2-> left); // The first right subtree of the second queue} If (leftqueue. size ()> 0 | rightqueue. size ()> 0) return false; return true ;}};

Sword refers to the image tree of offer. Question: enter a binary search tree to convert the tree to its image, that is, in the converted Binary Search Tree, the left subtree has more nodes than the right subtree. Use recursive and cyclic methods to convert tree images.

Analysis: Here is the conversion to the image tree. The idea is similar to the above judgment. Each time we process the Left and Right Subtrees separately, we can use recursion or iteration. The difference between this and the judgment is that the judgment must locate the location of the mirror node of each node, and the conversion does not need to be completed in one step, so each node needs to be exchanged with adjacent nodes, relatively simple. For iteration, you can use a queue or a stack. The Code is as follows:

Struct treenode {int val; treenode * left; treenode * right; treenode (int x): Val (x), left (null), right (null ){}}; void response rictreerecursive (treenode * root) // recursion. Note the difference between it and the image tree above {If (! Root) return; treenode * TMP = root-> left; root-> left = root-> right; root-> right = TMP; Revoke rictreerecursive (root-> left ); response rictreerecursive (root-> right);} void response rictreeiterative (treenode * root) // iteration {If (! Root) return; stack <treenode *> S; S. Push (Root); While (! S. empty () {treenode * pcur = S. top (); S. pop (); treenode * TMP = pcur-> left; pcur-> left = pcur-> right; pcur-> right = TMP; If (pcur-> left) s. push (pcur-> left); If (pcur-> right) s. push (pcur-> right );}}

Same Tree

Given two binary trees, write a function to check if they are equal or not.

Two Binary Trees are considered equal if they are structurally identical and the nodes have the same value.

Recursive Solution

struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { if(p == NULL && q == NULL)return true;if(p == NULL || q == NULL)return false;if(p->val == q->val)return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);else return false; }};

The iterative solution (similar to the above) is 7 times higher than the recursive algorithm

Struct treenode {int val; treenode * left; treenode * right; treenode (int x): Val (x), left (null), right (null ){}}; class solution {public: bool issametree (treenode * P, treenode * q) {If (! P &&! Q) return true; If (! P |! Q) return false; queue <treenode *> queue1, queue2; queue1.push (P), queue2.push (Q); While (queue1.size ()> 0 & queue2.size ()> 0) {treenode * pnode1 = queue1.front (), * pnode2 = queue2.front (); queue1.pop (), queue2.pop (); If (! Pnode1 &&! Pnode2) continue; If (! Pnode1 |! Pnode2) return false; If (pnode1-> Val! = Pnode2-> Val) return false; queue1.push (pnode1-> left), queue1.push (pnode1-> right); queue2.push (pnode2-> left ), queue2.push (pnode2-> right); // because the decision is the same tree, the team goes in the same order} If (queue1.size ()> 0 | queue2.size ()> 0) return false; return true ;}};

Leetcode-Based Structured tree image tree