Invert Binary Tree
Invert a binary tree.
recursion:
Class Solution {public
:
treenode* inverttree (treenode* root) {
if (root = null) return null;
Swap (Root->left, root->right);
Root->left = Inverttree (root->left);
Root->right = Inverttree (root->right);
return root;
}
};
Iteration
Class Solution {public
:
treenode* inverttree (treenode* root) {
if (root = null) return null;
queue<treenode*> Q;
Q.push (root);
while (!q.empty ()) {
treenode* node = Q.front ();
Q.pop ();
Swap (Node->left, node->right);
if (node->left) Q.push (node->left);
if (node->right) Q.push (node->right);
}
return root;
}
};
same Tree
Given binary trees, write a function to check if they is equal or not.
The binary trees is considered equal if they is structurally identical and the nodes has the same value.
/**
* 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 = = N ULL) return true;
if (p = = NULL | | q = NULL) return false;
if (p->val! = Q->val) return false;
Return Issametree (P->left, Q->left) && issametree (P->right, q->right);
}
};
symmetric Tree
Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).
Note:
Bonus points if you could solve it both recursively and iteratively.
DFS:
Class Solution {public
:
bool Issymmetric (treenode* root) {
return Dfs (root, root);
}
Private:
bool Dfs (treenode* R1, treenode* r2) {
if (r1 = = NULL && r2 = null) return true;
if (r1 = = NULL | | r2 = = NULL) return false;
if (r1->val! = R2->val) return false;
Return Dfs (R1->left, r2->right) && dfs (r1->right, r2->left);
}
;
Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes ' values.
Note:recursive solution is trivial, could do it iteratively?
Idea: Stack simulation: Output parent node, press stack right node, press stack left node
/**
* 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
:
vector<int> preordertraversal (treenode* root) {
Vector<int > res;
if (root = NULL) return res;
stack<treenode*> s;
S.push (root);
treenode* node;
while (!s.empty ()) {
node = s.top ();
S.pop ();
Res.push_back (node->val);
if (node->right) S.push (node->right);
if (node->left) S.push (node->left);
}
return res;
}
};
inorder Traversal
Given a binary tree, return the inorder traversal of its nodes ' values.
Note:recursive solution is trivial, could do it iteratively?
Write code here
Binary Tree level Order traversal
Given a binary tree, return the level order traversal of its nodes ' values. (ie, from left-to-right, level by level).
For example:
Given binary Tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
Return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
Idea: Queue simulation hierarchy traversal
Class Solution {public
:
vector<vector<int>> levelorder (treenode* root) {
Vector<vector <int> > Res;
if (root = NULL) return res;
queue<pair<treenode*, int> > Q;
treenode* N;
pair<treenode*, int> p;
Q.push (pair<treenode*, int> (root, 0));
while (!q.empty ()) {
p = q.front ();
Q.pop ();
n = p.first;
if (Res.size () <= p.second) {res.push_back (vector<int> ());}
Res[p.second].push_back (n->val);
if (n->left) {Q.push (pair<treenode*, int> (N->left, p.second+1));}
if (n->right) {Q.push (pair<treenode*, int> (N->right, p.second+1));}
} return res;
}
};
Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along, the shortest path from the root node to the nearest leaf node.
BFS:
Class Solution {public
:
int mindepth (treenode* root) {
if (root = NULL) return 0;
queue< pair<treenode*, int> > Q;
Q.push (pair<treenode*, int> (root, 1));
treenode* node;
pair<treenode*, int> p;
while (!q.empty ()) {
p = q.front ();
Q.pop ();
node = P.first;
if (Node->left = = NULL && Node->right = = null) return p.second;
if (node->left! = NULL) {Q.push (pair<treenode*, int> (Node->left, p.second+1));}
if (node->right! = NULL) {Q.push (pair<treenode*, int> (Node->right, p.second+1));}
} return-1;
}
};
DFS:
O
\
O
Root does not have a left subtree, but cannot take the depth of the left sub-tree
Class Solution {public
:
int mindepth (treenode* root) {
if (root = NULL) return 0;
if (Root->left = = NULL && Root->right = = null) return 1;
int leftdepth, rightdepth;
Leftdepth = Mindepth (root->left);
Rightdepth = Mindepth (root->right);
if (Root->left = = NULL) {return rightdepth+1;}
if (root->right = = NULL) {return leftdepth+1;}
return min (leftdepth, rightdepth) +1;
}
;
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along, the longest path from the root node to the farthest leaf node.
Dfs:
Class Solution {public
:
int maxDepth (treenode* root) {
if (root = = 0) return 0;
Return Max (MaxDepth (Root->left), maxDepth (root->right)) +1;
}
};
Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree was defined as a binary tree in which the depth of the subtrees of the Y node never differ by more than 1.
Idea: DFS
Solution One:
Judging from the smallest subtree, it is not balanced, and the judgment method is to calculate the height difference between the left and right subtree.
Because the tree has n nodes, the height of the tree is log (n). Each node is evaluated for a height, so the method has a time complexity of N*log (n)
Class Solution {public
:
bool isbalanced (treenode* root) {
if (root = NULL) return true;
int leftheight = getheight (root->left);
int rightheight = getheight (root->right);
if (ABS (Leftheight-rightheight) > 1) return false;
Return isbalanced (Root->left) && isbalanced (root->right);
}
int getheight (treenode* root) {
if (root = NULL) return 0;
Return Max (GetHeight (Root->left), GetHeight (root->right)) + 1;
}
};
Solution Two: For the solution one, add the marker, when finding the non-equilibrium subtree pruning stop search
Class Solution {public
:
bool isbalanced (treenode* root) {
if (root = NULL) return true;
bool ans = true;
Helper (root, ans);
return ans;
}
int helper (treenode* root, bool& ans) {
if (!root | |!ans) return 0;//ans = = false, cut-off
int Leftheigh T = Helper (root->left, ans);
int rightheight = Helper (root->right, ans);
if (ABS (Leftheight-rightheight) > 1) ans = false;
Return Max (Leftheight, rightheight) + 1;
}
};
Solution Three:
Space change time, saving the height of each subtree.
Time complexity O (1) *n = O (n), spatial complexity O (n)
Class Solution {public
:
bool isbalanced (treenode* root) {
um.clear ();
Um[null] = 0;
return Isbalancedhelper (root);
}
Private:
bool Isbalancedhelper (treenode* root) {
if (root = NULL) return true;
int leftheight = getheight (root->left);
int rightheight = getheight (root->right);
if (ABS (Leftheight-rightheight) > 1) return false;
Return Isbalancedhelper (Root->left) && isbalancedhelper (root->right);
}
int getheight (treenode* root) {
if (!root) return 0;
Um[root] = max (GetHeight (Root->left), GetHeight (root->right)) + 1; Ecursion to get height from subtree
return um[root];
}
unordered_map<treenode*, int> um; Addr of Sub Tree root, height
};
Solution Four:
Improved solution a height>=0 represents the subtree balance, and height represents the height of the subtree =-1 indicates that the subtree is unbalanced
Class Solution {public
:
bool isbalanced (treenode* root) {
return getheight (root) >= 0;
}
Private:
int getheight (treenode* root) {
if (!root) return 0;
int leftheight = getheight (root->left);
int rightheight = getheight (root->right);
if (Leftheight < 0 | | Rightheight < 0 | | ABS (LEFTHEIGHT-RIGHTHEIGHT) > 1) return-1;
Return Max (Leftheight, rightheight) + 1;
}
};
Construct Binary Tree from preorder and inorder traversal
Given Preorder and inorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
class Solution {public:treenode* Buildtree (vector<int>& preorder, vector<int&
gt;& inorder) {return helper (preorder, 0, Preorder.size ()-1, inorder, 0, Inorder.size ()-1); } private:treenode* Helper (vector<int>& preorder, int L1, int r1, vector<int>& inorder, int L2, I
NT R2) {if (R1 < L1) return NULL;
int rootval = PREORDER[L1];
TreeNode *root = new TreeNode (rootval);
int inorderrootpos = L2;
while (Inorder[inorderrootpos]! = rootval) {inorderrootpos++;}
int nodesnumofleft = INORDERROOTPOS-L2;
int nodesnumofright = Preorder.size () -1-nodesnumofleft;
Root->left = Helper (preorder, l1+1, L1+nodesnumofleft, Inorder, L2, inorderRootPos-1);
Root->right = Helper (preorder, l1+nodesnumofleft+1, R1, Inorder, inorderrootpos+1, r2);
return root; }
};
Construct Binary Tree from Inorder and Postorder traversal
Given Inorder and Postorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
class Solution {public:treenode* buildtree (vector<int>& inorder, VECTOR<INT&G
t;& postorder) {return helper (inorder, 0, Inorder.size ()-1, postorder, 0, Postorder.size ()-1); } private:treenode* Helper (vector<int>& inorder, int L1, int r1, vector<int>& postorder, int L2,
int R2) {if (R1 < L1) return NULL;
int rootval = POSTORDER[R2];
TreeNode *root = new TreeNode (rootval);
int inorderrootpos = L1;
while (Inorder[inorderrootpos]! = rootval) {inorderrootpos++;}
int nodesnumofleft = INORDERROOTPOS-L1;
int nodesnumofright = Inorder.size () -1-nodesnumofleft;
Root->left = Helper (inorder, L1, InorderRootPos-1, Postorder, L2, l2+nodesnumofleft-1);
Root->right = Helper (inorder, inorderrootpos+1, R1, Postorder, L2+nodesnumofleft, r2-1);
return root; }
};