前序遍历
如果该节点的右子树不为空,将右子节点加入栈中。 如果左子节点不为空,将左子节点加入栈中。
/**
* 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>result;
stack<TreeNode*>stack;
if(root!=NULL)
{
stack.push(root);
}
while(!stack.empty())
{
TreeNode* proof=stack.top();
stack.pop();
result.push_back(proof->val);
if(proof->right)
{
stack.push(proof->right);
}
if(proof->left)
{
stack.push(proof->left);
}
}
return result;
}
};
中序遍历
/**
* 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> inorderTraversal(TreeNode* root) {
vector<int>result;
stack<TreeNode*>stack;
TreeNode* p=root;
while(p||!stack.empty())
{
while(p)
{
stack.push(p);
p=p->left;
}
p=stack.top();
stack.pop();
result.push_back(p->val);
p=p->right;
}
return result;
}
};
层次遍历
int TreeDepth(TreeNode* pRoot)
{
queue<TreeNode*> q;
if(!pRoot) return 0;
q.push(pRoot);
int level=0;
while(!q.empty()){
int len=q.size();
level++;
while(len--){
TreeNode* tem=q.front();
q.pop();
if(tem->left) q.push(tem->left);
if(tem->right) q.push(tem->right);
}
}
return level;
}
路径总和
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
std::vector<std::vector<int>>result;
std::vector<int>path;
int path_value=0;
pre(root,sum,result,path,path_value);
return result;
}
private:
void pre(TreeNode* root,int sum,std::vector<std::vector<int>>&result,std::vector<int>&path,int &path_value)
{
if(!root)
return;
path_value+=root->val;
path.push_back(root->val);
if(!root->left&&!root->right&&path_value==sum)
result.push_back(path);
pre(root->left,sum,result,path,path_value);
pre(root->right,sum,result,path,path_value);
path_value-=root->val;
path.pop_back();
}
};
重建二叉树
链接:https://www.nowcoder.com/questionTerminal/8a19cbe657394eeaac2f6ea9b0f6fcf6
来源:牛客网
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) {
int inlen=in.size();
if(inlen==0)
return NULL;
vector<int> left_pre,right_pre,left_in,right_in;
//创建根节点,根节点肯定是前序遍历的第一个数
TreeNode* head=new TreeNode(pre[0]);
//找到中序遍历根节点所在位置,存放于变量gen中
int gen=0;
for(int i=0;i<inlen;i++)
{
if (in[i]==pre[0])
{
gen=i;
break;
}
}
//对于中序遍历,根节点左边的节点位于二叉树的左边,根节点右边的节点位于二叉树的右边
//利用上述这点,对二叉树节点进行归并
for(int i=0;i<gen;i++)
{
left_in.push_back(in[i]);
left_pre.push_back(pre[i+1]);//前序第一个为根节点
}
for(int i=gen+1;i<inlen;i++)
{
right_in.push_back(in[i]);
right_pre.push_back(pre[i]);
}
//和shell排序的思想类似,取出前序和中序遍历根节点左边和右边的子树
//递归,再对其进行上述所有步骤,即再区分子树的左、右子子数,直到叶节点
head->left=reConstructBinaryTree(left_pre,left_in);
head->right=reConstructBinaryTree(right_pre,right_in);
return head;
}
};