Binary Tree - Summary examples
1- therefrom sequence order and postorder binary tree structure
Given the binary tree traversal sequence after sequence and binary tree traversal
idea:
- According to the last element of the root node after traversing configured
- Find the position of the root node in a preorder traversal of
- Recursive construction of left and right subtrees of the root 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:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0 || postorder.size() == 0)
return NULL;
int _is = 0;
int _ie = inorder.size()-1;
int _ps = 0;
int _pe = postorder.size()-1;
return build(inorder,postorder,_is,_ie,_ps,_pe);
}
// 构建节点的递归函数
TreeNode* build(vector<int>& inorder, vector<int>& postorder,int is,int ie,int ps,int pe)
{
// 构建根节点
TreeNode* ans = new TreeNode(postorder[pe]);
int ll = 0;
int rl = 0;
for(int i = is ; i <= ie ; ++i )
{
if(inorder[i] == postorder[pe])
{
// 左子树长度
ll = i - is;
// 右子树长度
rl = ie - i;
}
}
// 构建左子树
if ( ll > 0 )
{
ans->left = build(inorder,postorder,is,is+ll-1,ps,ps+ll-1);
}
// 构建右子树
if ( rl > 0 )
{
ans->right = build(inorder,postorder,ie-rl+1,ie,pe-rl,pe-1);
}
return ans;
}
};
to sum up:
- The return type for the pointer, anomalies can be direct return NULL
- The above code uses two variables, ll and rl respectively, left and right subtrees vector length inside.
- Each time the function is called, inclusive subscript changed with the two containers ll and rl.
2- previous order and a binary tree configuration sequence preorder
idea:
- According to the last element of the root node configured preorder traversal
- Find the position of the root node in a preorder traversal of
- Recursive construction of left and right subtrees of the root 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:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return build(inorder,preorder,0,inorder.size()-1,0,preorder.size()-1);
}
TreeNode* build(vector<int>& inorder,vector<int>& preorder, int is,int ie,int ps,int pe)
{
if(inorder.size()==0 || preorder.size()==0)
{
return NULL;
}
int ll = 0 ;
int rl = 0 ;
TreeNode* ans = new TreeNode(preorder[ps]);
for(int i = is; i<= ie; i++)
{
if(preorder[ps]==inorder[i])
{
ll = i - is ;
rl = ie - i;
}
}
if ( ll > 0 )
{
ans->left = build (inorder,preorder,is,is+ll-1,ps+1 ,ps+ll );
}
if ( rl > 0 )
{
ans->right = build(inorder,preorder,ie-rl+1,ie,pe-rl+1,pe);
}
return ans;
}
};
3- filling the next right node pointer of each node (perfect binary)
idea:
- By traverse the level, use the Queue
- The last node points to each layer of the next, otherwise point to the next
class Solution {
public:
Node* connect(Node* root) {
if( root == NULL)
return NULL;
queue<Node*> q;
q.push(root);
// 记录每一层的元素个数
while( ! q.empty())
{
int num = q.size();
// 遍历当前层(队列)里面的每个元素
for(int i = 0; i < num; i++)
{
// p指向 是队列的头节点
Node* p = q.front();
// 出队
q.pop();
// 如果到当前层最后一个元素了,next指针指向NULL,队未空,next指向队头节点
if(i == num-1)
p->next = NULL;
else
p->next = q.front();
// p 的左右孩子节点入队
if( p->left != NULL )
q.push( p->left );
if ( p->right != NULL )
q.push( p->right );
}
}
return root;
}
};
The next right node pointer 4- filled each node (non-perfect binary tree)
My solution above.
class Solution {
public:
Node* connect(Node* root) {
if( root == NULL)
return NULL;
queue<Node*> q;
q.push(root);
// 记录每一层的元素个数
while( ! q.empty())
{
int num = q.size();
// 遍历当前层(队列)里面的每个元素
for(int i = 0; i < num; i++)
{
// p指向 是队列的头节点
Node* p = q.front();
// 出队
q.pop();
// 如果到当前层最后一个元素了,next指针指向NULL,队未空,next指向队头节点
if(i == num-1)
p->next = NULL;
else
p->next = q.front();
// p 的左右孩子节点入队
if( p->left != NULL )
q.push( p->left );
if ( p->right != NULL )
q.push( p->right );
}
}
return root;
}
};
5- binary tree common ancestor
own thoughs:
- Respectively in the tree to find the target node. The Pathfinder is stored in two stacks inside.
- One stack the stack in order to find this out in another node stack in a stack.
- Note: Because the search path is unique.
/**
* 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
stack<TreeNode*> A;
stack<TreeNode*> B;
find(root,p,A);
find(root,q,B);
vector<int> bb;
for(int i = 0; i< B.size();++i)
{
bb.push_back(B.top()->val);
B.pop();
}
while(!A.empty())
{
TreeNode* ans = A.top();
for(int i = 0 ; i< bb.size();++i)
{
if( ans->val == bb[i])
return ans;
}
}
return NULL;
}
void find (TreeNode* root ,TreeNode* target, stack<TreeNode*> &ss)
{
if (! root)
return ;
if(root->val == target->val)
{
ss.push(root);
}
if(root->left != NULL)
{
vector<int> lv = dfs(root->left);
for(int i = 0; i < lv.size() ; ++i )
{
if(lv[i] == target->val)
{
ss.push(root->left);
find(root->left,target ,ss);
}
}
}
if(root->right != NULL)
{
vector<int> rv = dfs(root->right);
for(int i = 0; i < rv.size() ; ++i )
{
if(rv[i] == target->val)
{
ss.push(root->right);
find(root->right,target,ss);
}
}
}
}
vector<int> dfs(TreeNode* root)
{
vector<int> order;
helper(root,order);
return order;
}
void helper( TreeNode* root, vector<int>& vv)
{
if(root->left!=NULL)
helper(root->left,vv);
vv.push_back(root->val);
if(root->right != NULL)
helper(root->right,vv);
}
};
Code is out of time limit
Look at other people's code for it:
/**
* 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || !p || !q)
return NULL;
vector<TreeNode*> A;
vector<TreeNode*> B;
dfs(root,p,A);
dfs(root,q,B);
TreeNode* ans ;
int len = min(A.size(),B.size());
for(int i = 0; i < len ; i++)
{
if(A[i]->val != B[i]->val)
break;
ans = A[i];
}
return ans;
}
bool dfs(TreeNode* root,TreeNode* target,vector<TreeNode*>& path)
{
if( root == target ){
path.push_back(root);
return true;
}
path.push_back(root);
if( root->left && dfs( root->left , target,path ))
return true;
if(root->right && dfs( root->right , target , path ))
return true;
// 回溯???
path.pop_back();
return false;
}
};
problem:
- == depth traversal function in, pop_back () understanding: backtracking? ? ? ==
- for circulation problems
for(int i = 0; i < len ; i++)
{
if(A[i]->val != B[i]->val)
break;
ans = A[i];
}