Preface:
first, why should the tree structure. Unlike arrays, linked lists are linear data structures, a tree is a layered, non-linear structure (1) One reason to use a tree is that we need to store information that has hierarchical relationships (such as file systems) (2) Another is (BST): When the tree is built with a certain form of tree can be convenient for data search (for the balanced tree, Find time complexity of O (Logn)). (3) Similarly for such a tree (avl/red-black tree): Their insertion and deletion of the time complexity is (O (Logn)) (4) relative to the array, the tree uses pointer operation, you can dynamically expand the node.
two or two the BFS and DFS of the fork tree1. A tree is typically traversed in two ways: breadth-first traversal (or sequence traversal) depth-first traversal sequence traversal sequential traversal 2, four-way comparison (1) time complexity, the above four traversal methods need O (n) time, because they all traverse each node (2) Space complexity, for the BFS space Complexity of O (W) where w is the maximum width of the two-fork tree, in sequence traversal, using the queue to store different levels of nodes for DFS the space complexity is O (h) where h is the maximum height of the two fork tree, in depth traversal, Use the stack to store their ancestors nodes 3, how to choose a traversal method. The height of the balanced binary tree is O (logn), the worst case is when the tilt tree becomes O (n) extra space is a choice of standard DFS generally uses recursive methods, and the cost of recursive method function calls is also a factor most importantly: BFS always starts from the root node, DFS is more inclined to start with a leaf node, so if our problem is to find a location close to the root node we need to use BFS instead of Dfs.
Some features of the three or two fork tree(1) The number of nodes on layer i is 2^ (i-1), that is, the number of nodes on the I layer is twice times on the I-1 layer (2) the number of all nodes before layer i is 2^i-1; (1+2+4+7+2^ (I-1)) (3) If the binary tree also has n nodes, The minimum possible height or minimum number of layers is (log2 (n+1)) (4) A binary tree has l leaf nodes so at least there is a log2l+1 layer
(5) A node with a binary tree with a degree of 0 is a node with a ratio of 2 to a fully binary tree:
Full two fork tree (node has a degree of 0 and a degree of 2, here and I understand the difference before: )
Perfect two-prong tree
four or one some exercises and code
Annotations1, the number of nodes in the binary tree (222) 2, to find the depth of the binary tree (104) to find the minimum depth of the binary tree (111) 3, the pre-order traversal (94/144/145) 4, Layered traversal binary tree (102/107) zigzag Traverse (103) 5, the two-fork lookup tree into an ordered doubly linked list (114) 6, the number of K-level nodes of the binary tree 7, the number of leaf nodes in the binary tree 8, to determine whether the two binary tree structure is the same (100) 9, determine whether the binary tree is a balanced binary tree (110) 10, seek the image of the binary tree (226) to determine whether the binary tree is symmetrical (101)
<span style= "line-height:1.5; font-family: ' Courier New '; Background-color:inherit; " >11 Minimum Common ancestor node (236) for two nodes of a binary tree </span>
12, the maximum distance of the nodes in the binary tree 13, by the pre-sequence traversal and the middle sequence traversal reconstruction of the binary tree (105) by the middle order and post-order traversal reconstruction of the binary tree (106) 14, determine whether the binary tree is a complete binary tree () 15, an ordered array into a two-fork search tree (108)
#include <iostream> #include <queue> #include <vector> #include <stack> #include <algorithm&
Gt
using namespace Std;
struct treenode{int val;
Treenode* left;
treenode* right;
TreeNode (int n): Val (n), left (nullptr), right (nullptr) {}};
1. Find the number of nodes in the binary tree//violent recursion is used for normal two-fork-tree int countnodes (treenode* root) {if (root = NULL) return 0;
Return Countnodes (Root->left) + countnodes (root->right) + 1;
}//For a complete binary tree you can use the formula 2^h-1 int getleft (treenode* root) {int count = 0;
while (root->left! = NULL) {root = root->left;
count++;
} return count;
} int GetRight (treenode* root) {int count = 0;
while (root->right! = NULL) {root = root->right;
count++;
} return count;
} int CountNodes2 (treenode* root) {if (root = NULL) return 0;
int leftcount = GetLeft (root) +1;
int rightcount = GetRight (root) +1; if (Leftcount = = Rightcount) return (2 << (leftcount-1))-1;//here Else return CountNodes2 (root->left) + Coun TNodes2 (ROOT->right) + 1;
}//2, find the depth of the//2.1 to find the maximum depth of the binary tree int maxDepth (treenode* root) {if (root = NULL) return 0;
int maxleft = maxDepth (root->left);
int maxright = maxDepth (root->right); return maxleft > Maxright?
(Maxleft + 1): (Maxright + 1);
}//2.2 the minimum depth of the binary tree int mindepth (treenode* root) {if (root = NULL) return 0;
if (Root->left = = Null&&root->right = = NULL) return 1;
int left = mindepth (Root->left);
int right = Mindepth (root->right);
if (Root->left = = NULL) return right + 1;
if (root->right = = NULL) return left + 1; Return left > right?
(right + 1): (left + 1);
} int minDepth2 (treenode* root) {queue<pair<treenode*, int> >q;
if (root = NULL) return 0;
Q.push (Make_pair (root, 1));
while (!q.empty ()) {pair<treenode*, int> cur = q.front ();
Q.pop ();
if (Cur.first->left = = Null&&cur.first->right = = NULL)//Returns a return Cur.second when encountering the first leaf node; if (cur.first->left) Q.push (Make_pair (cur.first->left,cur.second+1));
if (cur.first->right) Q.push (Make_pair (cur.first->right, Cur.second + 1));
}}//3, before the post-sequential traversal//3.1 recursive traversal vector<int> VEC;
Vector<int> inordertraversal (treenode* root) {if (root = NULL) return VEC;
Inordertraversal (Root->left);
Vec.push_back (Root->val);
Inordertraversal (Root->right);
return VEC;
} vector<int> preordertraversal (treenode* root) {if (root = NULL) return VEC;
Vec.push_back (Root->val);
Preordertraversal (Root->left);
Preordertraversal (Root->right);
return VEC;
} vector<int> postordertraversal (treenode* root) {if (root = NULL) return VEC;
Postordertraversal (Root->left);
Postordertraversal (Root->right);
Vec.push_back (Root->val);
return VEC;
}//3.2 non-recursive version vector<int> inordertraversal (treenode* root) {stack<treenode*> s;
treenode* temp = root;
while (temp! = null| |!s.empty ()) {while (temp! = NULL) {s.push (temp); temp = temp->Left
} if (!s.empty ()) {temp = S.top ();
S.pop ();
Vec.push_back (Temp->val);
temp = temp->right;
}} return VEC;
} vector<int> preordertraversal (treenode* root) {stack<treenode*> s;
treenode* temp = root;
while (temp | |!s.empty ()) {while (temp! = NULL) {vec.push_back (temp->val);
S.push (temp);
temp = temp->left;
} if (!s.empty ()) {temp = S.top ();
temp = temp->right;
S.pop ();
}} return VEC;
} vector<int> postordertraversal (treenode* root) {if (root = NULL) return VEC;
return VEC;
}//4, Layered traversal binary tree//4.1 vector<vector<int>> levelorder (treenode* root) {vector<treenode*> q;
Vector<vector<int> >res;
if (root = NULL) {q.push_back (root);
int cur = 0;
int last = 1;
while (cur < q.size ()) {last = Q.size ();
Vector<int> VEC;
while (cur < last) {Vec.push_back (q[cur]->val); if (q[cur]->left) q.push_back (q[Cur]->left];
if (q[cur]->right) q.push_back (q[cur]->right);
cur++;
} res.push_back (VEC);
}} return res;
}//4.2 Glyph Print binary tree vector<vector<int>> zigzaglevelorder (treenode* root) {vector<treenode*> q;
Vector<vector<int> >res;
if (root = NULL) {q.push_back (root);
int cur = 0;
int last = 1;
int row = 1;
while (cur < q.size ()) {last = Q.size ();
Vector<int> VEC;
while (cur < last) {Vec.push_back (q[cur]->val);
if (q[cur]->left) q.push_back (q[cur]->left);
if (q[cur]->right) q.push_back (q[cur]->right);
cur++;
} if (row% 2 = = 0) {reverse (Vec.begin (), Vec.end ());
} row++;
Res.push_back (VEC);
}} return res; }///5, turn the two-fork lookup tree into an ordered doubly linked list//6, find the number of K-level nodes of the binary tree int numofkthlevel (treenode* root, int k) {if (root = NULL | | K < 1) return
0;
if (k = = 1) return 1;
int numleft = Numofkthlevel (Root->left, k-1); int numright = numOfkthlevel (Root->right, k-1);
return numleft + numright;
}//7, find the number of leaf nodes in the binary tree int numofleaf (treenode* root) {if (root = NULL) return 0;
BOOL IsLeaf = Root->left = = Null&&root->right = = NULL;
if (isleaf) return 1;
int numleft = Numofleaf (root->left);
int numright = Numofleaf (root->right);
return numleft + numright;
}//8, judging whether the structure of the two binary trees is the same bool Issametree (treenode* p, treenode* q) {if (p = = Null&&q = NULL) return true; if ((p = = null&&q! = NULL) | |
(P! = Null&&q = = NULL))
return false;
if (p->val! = Q->val) return false;
else {bool L = Issametree (P->left, q->left);
BOOL R = Issametree (P->right, q->right);
if (L&&r) return true;
else return false;
}}//9, determine if the binary tree is balanced binary tree bool isbalanced (treenode* root) {if (root = NULL) return true;
int* depth = 0;
return IsBalanced2 (root, depth);
} bool IsBalanced2 (treenode* root, int *depth) {if (root = = NULL) {depth = 0; return TRUe
} int nleft, nright;
BOOL RL = ISBALANCED2 (Root->left, &nleft);
BOOL rr = IsBalanced2 (Root->right, &nright);
if (RL&&RR) {int diff = nleft-nright; if (diff >=-1 && diff <= 1) {*depth = nleft > nright?
(Nleft + 1): (Nright + 1);
return true;
}} return false;
}//10, the image of the binary tree treenode* inverttree (treenode* root) {if (root = null) return null;
treenode* temp = root->left;
Root->left = root->right;
Root->right = temp;
Root->left = Inverttree (root->left);
Root->right = Inverttree (root->right);
return root;
//10.2 Determines whether a tree is symmetric bool Issymmetric (treenode* root) {if (root = NULL) return true;
Return IsSymmetric2 (Root->left, root->right);
} bool IsSymmetric2 (treenode* root1, treenode* root2) {if (root1 = = Null&&root2 = NULL) return true; if ((root1! = Null&&root2 = = NULL) | |
(ROOT1 = = null&&root2! = NULL))
return false; if (root1->val! = root2-≫val) return false;
bool L = IsSymmetric2 (Root1->left, root2->right);
BOOL R = IsSymmetric2 (Root1->right, root2->left);
Return l&&r;
}//11, the lowest common ancestor node of the binary tree two nodes is//12, the maximum distance of the nodes in the binary tree is//13, the binary tree typedef vector<int>::iterator Iter is reconstructed by the pre-sequence traversal and the middle sequence traversal; treenode* Buildtree (vector<int>& preorder, vector<int>& inorder) {if (preorder.size () = = 0 | | inorde R.size () = = 0| |
Preorder.size ()!=inorder.size ()) return NULL;
Return BuildTree2 (Preorder.begin (), Preorder.end (), Inorder.begin (), Inorder.end ()); } treenode* BuildTree2 (Iter Pbegin, ITER Pend, Iter Ibegin, iter iend) {if (Pbegin = = Pend | | ibegin = iend) return N
ULL;
treenode* root = new TreeNode (*pbegin);
Auto Iroot = Find (Ibegin, iend, *pbegin);
int leftlength = Iroot-ibegin;
Root->left = BuildTree2 (pbegin+1,pbegin+leftlength+1, ibegin,iroot);
Root->right = buildTree2 (pbegin + leftlength + 1,pend, iroot+1,iend);
return root; }//reconstruction of the binary tree typedef by order and sequence Vector<int>::iteratoR Iter; treenode* BuildTree2 (Iter Pbegin, ITER Pend, Iter Ibegin, iter iend) {if (Pbegin = = Pend | | ibegin = iend) return NUL
L
treenode* root = new TreeNode (* (pend-1));
Auto Iroot = Find (Ibegin, Iend, * (pend-1));
int leftlength = Iroot-ibegin;
Root->left = BuildTree2 (pbegin, Pbegin + leftlength, Ibegin, iroot);
Root->right = buildTree2 (Pbegin + leftlength, pend-1, Iroot + 1, iend);
return root; } treenode* Buildtree (vector<int>& inorder, vector<int>& postorder) {if (postorder.size () = = 0 | | in Order.size () = = 0 | |
Postorder.size ()! = Inorder.size ()) return NULL;
Return BuildTree2 (Postorder.begin (), Postorder.end (), Inorder.begin (), Inorder.end ()); }//14, judging whether the binary tree is a complete binary tree//15, the ordered array is composed of two forks to find the tree treenode* sortedarraytobst (vector<int>& nums) {if (Nums.empty ()) R
Eturn NULL;
treenode* root = new TreeNode (Nums[nums.size ()/2]);
Vector<int> Pre (Nums.begin (), Nums.begin () + nums.size ()/2); Vector<int> Last (Nums.begiN () + nums.size ()/2 + 1, nums.end ());
Root->left = Sortedarraytobst (pre);
Root->right = Sortedarraytobst (last);
return root; }
Reference article: Http://blog.csdn.net/luckyxiaoqiang/article/details/7518888#topic2 and blogs about some leetcode topics