BST——depth, same,symmetric,path sum

Symmetric Tree

 


//受到剑指offer上输出树的镜像树的题目的影响,一心想要构建一颗树的镜像树再进行比较可是
//发现好难实现,其次就是交换树的左右子节点的值,这样是不对的,对称的意思是左右子树都完全对称,镜像不是对称只是将
//树左右子树交换,没有判断是不是对称的,
//另外,判断树是否对称时,需要考虑到两个节点,不知道怎么从一个节点转化成两个节点的判断,看了解答,发现就是传递两个指针就好了
//对称性体现在左边的左边和右边的右边等,左边的右边和右边的左边等
bool check(TreeNode* left, TreeNode* right)
{
	if(left == NULL && right == NULL)
		return true;
	if(left == NULL ||right == NULL)
		return false;
	if(left->val == right->val)
		return check(left->left,right->right) && check(left->right,right->left);
	return false;
}



bool isSymmetric(TreeNode *root) 
{
	if(root == NULL)return true;
	return check(root->left,root->right);
}


Same Tree

class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(p == NULL && q == NULL)
            return true;
        if(p!=NULL && q!=NULL && p->val == q->val)
            return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
        return false;
    }
};





Path Sum

 

//犯得致命的错误就是,觉得sum是正数,题目没说,他可正可负,不能直接判断
//其次,就是是从root到leaf的路径,所以如果只用node == NULL(可能是内节点)判断是不对的,leaf的定义是
//leaf-left == NULL && leaf-right == NULL才是叶子,
bool PathSum(TreeNode* node, int& sum)
{
	int temp = sum - node->val;
	if(temp == 0 && node->left ==NULL && node->right == NULL)//是叶子节点
	    return true;
	if(node->left != NULL && node->right !=NULL)
		return PathSum(node->left,temp) || PathSum(node->right,temp);
	if(node->left != NULL)
		return PathSum(node->left,temp);
	if(node->right != NULL)
		return  PathSum(node->right,temp);

	return false;
}
bool hasPathSum(TreeNode* root, int sum)
{
	if(root == NULL )
		return false;

	return PathSum(root,sum);
}

不够简洁,因为其实单独对节点一个null判断就可以避免那么多判断if_else,只在叶子节点且满足sum输出,重新编写

class Solution {
public:

bool hasPathSum(TreeNode* root, int sum)
{
	if(root == NULL)
		return false;
	int temp = sum-root->val;
	if(temp == 0 && root->left == NULL && root->right == NULL)
		return true;
	return hasPathSum(root->left,temp) || hasPathSum(root->right,temp);
}
};


Path Sum II

 

注意点就是path在压入节点后,节点处理完后,要弹出该节点,其次sum每次计算减法后,结束一个节点的计算要加回去。
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
void PathSum(vector<vector<int> >& all,vector<int>& path, TreeNode* node, int& sum)
{
	if(node == NULL)
		return;
	int temp = sum - node->val;
	if(temp == 0 && node->left ==NULL && node->right == NULL)//是叶子节点
	{
		path.push_back(node->val);
		all.push_back(path);
		path.pop_back();
		sum = temp+node->val;
		return;
	}
	path.push_back(node->val);

	PathSum(all, path,node->left,temp);

	PathSum(all, path,node->right,temp);

	path.pop_back();

	sum = temp+node->val;

}
vector<vector<int> > pathSum(TreeNode* root, int sum)
{
	vector<vector<int> > allPaths;
	vector<int> path;
	if(root == NULL )
		return allPaths;
	PathSum(allPaths,path,root,sum);

	return allPaths;
}
};


Binary Tree Maximum Path Sum

 


//太简练了,我想得办法是根据左右子树的返回值,计算6个值的最大值,替换maxVal,可是因为使用INT_MIN,如果有负数会
//越界出错,(maxVal保存的是遍历过程中可能产生的最大值,所以根,左,右,左根,右根,左根右)因为我的max(root->val,l,r,l+val,r+val,r+l+val),
 //所以考虑分情况讨论就是分成左右子树都是空,
//左子树空,右子树空,都不为空四种情况进行local取最大值,然后覆盖maxVal,最后返回的结果因为必须是路径上的最大值
 //所以再次在l+val,r+val,r中取最大值返回,
 //别人的代码简练在,不是每种情况都详细的列举,只是深入到内部,找到判断的本质条件,在可能的6种情况实际上根本都是
 //突然发现,自己思考的6种情况是不对的,因为maxVal总是记录已经遍历过得子树中的最大值,所以实际情况是4种,根,左根,右根,左右根,因为
 //最大是左或者右的情况是在maxVal中保存的
 //出发点local = root->val;然后我希望得到一个大值,不要4种情况都判断,只有对left和right>0做出判断,大于0累加上去才有意义,才是4种情况
 //中的最大值,
 //其次,返回值是该子树的一个可能与调用的根相关联的最大路径,左根,右根,根,也是一样的道理,取左右最大值,然后与0比较取最大值,我只是
 //需要最大的情况,小于0不被考虑,加上根返回。太精妙了。
int PathSum(TreeNode* root, int &maxVal)
{
	if(root == NULL)
		return INT_MIN;

	int leftVal = PathSum(root->left, maxVal);
	int rightVal = PathSum(root->right, maxVal);
	
	int local = root->val;
	if(leftVal > 0)
		local += leftVal;

	if(rightVal > 0)
		local += rightVal;
	
	if(local > maxVal)
		maxVal = local;
	int maxbet = max(leftVal,rightVal);
	maxbet = max(maxbet, 0 ) + root->val;
	return maxbet;
}


int maxPathSum(TreeNode* root)
{
	if(root == NULL)
		return 0;
	int maxVal = INT_MIN;
	PathSum(root, maxVal);
	return maxVal;
}

Maximum Depth of Binary Tree


class Solution {
public:
int maxDepth(TreeNode* root)
{
	if(root == NULL) 
		return 0;
	int left_h = maxDepth(root->left);
	int right_h = maxDepth(root->right);
	return max(left_h,right_h)+1;
}
};

Minimum Depth of Binary Tree

 

自己写的,因为没有通过,发现最小深度应该是从根部到叶子的必须保证到叶子,所以如果有的节点左边或者右边高度为0,就取另一边的高度,保证是到达叶子的,但思维过程不好记忆。

class Solution {
public:
    int minDepth(TreeNode *root) {
        if(root == NULL) 
		    return 0;
	    int left_h = minDepth(root->left);
	    int right_h = minDepth(root->right);
	    if(left_h == 0 )
	        return right_h+1;
	    if(right_h == 0)
	        return left_h+1;
	    return min(left_h,right_h)+1;
    }
};


参考别人的代码
class Solution {
public:
    int minDepth(TreeNode *root) {
        if(root == NULL) 
		    return 0;
	if(root->left == NULL) return minDepth(root->right)+1;
	if(root->right == NULL) return minDepth(root->left)+1;
	int left_h = minDepth(root->left);
	int right_h = minDepth(root->right);
	return min(left_h,right_h)+1;
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值