前序遍历求树的深度,后序遍历求树的高度。深度是从根节点出发计算,高度是从叶子结点出发开始计算。
后序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//求高度,后序
int getHeight(TreeNode* cur){//确定递归函数的参数和返回值
//确定终止条件
if(cur==nullptr) return 0;
//确定单层递归逻辑
int leftNum=getHeight(cur->left);
int rightNum=getHeight(cur->right);
if(leftNum==-1) return -1;
if(rightNum==-1) return -1;
if(abs(leftNum-rightNum)>1) return -1;
else return max(leftNum,rightNum)+1;
}
bool isBalanced(TreeNode* root) {
int res=getHeight(root);
if(res==-1) return false;
else return true;
}
};
与求深度相似,用前序
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//确定递归函数参数和返回值
void travel(TreeNode* cur,vector<int>& path,vector<string>&res){
path.push_back(cur->val);//应该在递归结束前把叶子结点也加入path
//确定终止条件
if(cur->left==nullptr&&cur->right==nullptr){
string spath="";
for(int i=0;i<path.size()-1;i++){
spath+=to_string(path[i]);
spath+="->";
}
spath+=to_string(path[path.size()-1]);
res.push_back(spath);
}
//确定单层递归逻辑
if(cur->left){
travel(cur->left,path,res);
path.pop_back();//pop的是cur->left->val
}
if(cur->right){
travel(cur->right,path,res);
path.pop_back();
}
return;
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int>path;
vector<string>res;
travel(root,path,res);
return res;
}
};
自己写的:(其实暗含的有终止条件,因为我都加了if条件判断了,只要不满足就不会执行if后的语句,而且主要函数是void类型,走到函数整体结尾就结束了。)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res;
void traversal(TreeNode* cur){//确定递归函数参数和返回值
//确定终止条件
//确定单层递归逻辑
if(cur->left!=nullptr&&cur->left->left==nullptr&&cur->left->right==nullptr){
res+=cur->left->val;
}
if(cur->left){
traversal(cur->left);
}
if(cur->right){
traversal(cur->right);
}
}
int sumOfLeftLeaves(TreeNode* root) {
if(root==nullptr) return 0;
res=0;
traversal(root);
return res;
}
};
后序遍历:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {//确定递归函数参数和返回值
//确定终止条件
if(root==nullptr) return 0;//节点为空 -1层
if(root->left==nullptr&&root->right==nullptr) return 0;
//节点的左右孩子为空 最后一层,叶子节点
//确定单层递归逻辑
int leftNum=sumOfLeftLeaves(root->left);
if(root->left&&!root->left->left&&!root->left->right){
leftNum=root->left->val;
}
int rightNum=sumOfLeftLeaves(root->right);
int sum=leftNum+rightNum;
return sum;
}
};
层序遍历:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
int res=0;
if(root==nullptr) return 0;
que.push(root);
while(!que.empty()){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
if(i==0) res=cur->val;
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return res;
}
};
递归法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res;
int maxDepth=INT_MIN;
void traversal(TreeNode* cur,int depth){//确定递归函数参数和返回值
//确定终止条件
if(cur->left==nullptr&&cur->right==nullptr){
if(depth>maxDepth){
res=cur->val;
maxDepth=depth;
}
return;
}
//确定单层递归逻辑
if(cur->left){
traversal(cur->left,depth+1);
}
if(cur->right){
traversal(cur->right,depth+1);
}
return;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root,0);
return res;
}
};
递归法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool traversal(TreeNode* root,int sum){//确定递归函数参数和返回值
//确定终止条件
if(!root->left&&!root->right&&sum==0) return true;
if(!root->left&&!root->right&&sum!=0) return false;
//确定单层递归逻辑
if(root->left){
if(traversal(root->left,sum-root->left->val)) return true;
}
if(root->right){
if(traversal(root->right,sum-root->right->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr) return false;
return traversal(root,targetSum-root->val);
}
};
迭代法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr) return false;
stack<pair<TreeNode*,int>> st;
st.push(pair<TreeNode*,int>(root,root->val));
while(!st.empty()){
pair<TreeNode*,int> cur=st.top();
st.pop();
if(!cur.first->left&&!cur.first->right&&cur.second==targetSum) return true;
if(cur.first->left){
st.push(pair<TreeNode*,int>(cur.first->left,cur.second+cur.first->left->val));
}
if(cur.first->right){
st.push(pair<TreeNode*,int>(cur.first->right,cur.second+cur.first->right->val));
}
}
return false;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//确定递归函数参数和返回值
TreeNode* traversal(vector<int>& inorder,vector<int>& postorder){
//确定终止条件
if(postorder.size()==0) return nullptr;
//单层递归逻辑
//后序数组的最后一个值为中间值
int rootvalue=postorder[postorder.size()-1];
TreeNode* root=new TreeNode(rootvalue);
//叶子结点
if(postorder.size()==1) return root;
//第三步:找切割点
int divideIndex;
for(int i=0;i<inorder.size();i++){
if(inorder[i]==rootvalue) divideIndex=i;
}
//第四步:切割中序数组 左闭右开
vector<int> leftInorder(inorder.begin(),inorder.begin()+divideIndex);
vector<int> rightInorder(inorder.begin()+divideIndex+1,inorder.end());
//第五步: 切割后序数组
postorder.resize(postorder.size()-1);
vector<int> leftPostoder(postorder.begin(),postorder.begin()+leftInorder.size());
vector<int> rightPostorder(postorder.begin()+leftInorder.size(),postorder.end());
//第六步: 递归
root->left=traversal(leftInorder,leftPostoder);
root->right=traversal(rightInorder,rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return traversal(inorder,postorder);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//确定递归参数和返回值
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
//确定终止条件
TreeNode* node=new TreeNode(0);
if(nums.size()==1){
node->val=nums[0];
return node;
}
//确定单层递归逻辑
int maxValue=0;
int maxIndex;
for(int i=0;i<nums.size();i++){
if(nums[i]>maxValue) {
maxValue=nums[i];
maxIndex=i;
}
}
node->val=maxValue;
//构造左子树
if(maxIndex>0){
vector<int> leftNums(nums.begin(),nums.begin()+maxIndex);
node->left=constructMaximumBinaryTree(leftNums);
}
//构造右子树
if(maxIndex<nums.size()-1){
vector<int> rightNums(nums.begin()+maxIndex+1,nums.end());
node->right=constructMaximumBinaryTree(rightNums);
}
return node;
}
};
改进:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//改进:切割时不构造新数组
//确定递归函数和参数值
TreeNode* traversal(vector<int>& nums,int left,int right){
//确定终止条件
if(left==right) return nullptr;
//确定单层递归逻辑
int maxIndex=left;
for(int i=left+1;i<right;i++){
if(nums[i]>nums[maxIndex]) maxIndex=i;
}
TreeNode* node=new TreeNode(nums[maxIndex]);
node->left=traversal(nums,left,maxIndex);
node->right=traversal(nums,maxIndex+1,right);
return node;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums,0,nums.size());
}
};