Invert two fork 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* inverttree (treenode* root) { if (root==null) return NULL; if (root->left==null && root->right==null) return root; treenode* tmpnode=root->right; root->right=root->left; root->left=tmpnode; if (root->left) { root->left=inverttree (root->left); } if (root->right) { root->right=inverttree (root->right); } return root; }};
Determine if it is the same binary 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: 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;}} ;
Leetcode: Invert Binary tree and Same tree