算法练习:二叉树

102:二叉树的层序遍历

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

class Solution {
public:
    vector> levelOrder(TreeNode* root) {
        vector>arr1;
        queuequeue;
        if(root!=nullptr){queue.push(root);}
        while(!queue.empty())
        {
            int size = queue.size();
            vectorarr2;
            for(int i = 0;ival);
                queue.pop();
                if(ptr->left != nullptr)
                {
                    queue.push(ptr->left);
                }
                if(ptr->right != nullptr)
                {
                    queue.push(ptr->right);
                }
            }
            arr1.push_back(arr2);
        }
        return arr1;
    }
};

226:翻转二叉树

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        queuequeue;
        if(root!=nullptr){queue.push(root);}
        while(!queue.empty())
        {
                TreeNode* ptr = queue.front();
                if(ptr->left != nullptr)
                {
                    queue.push(ptr->left);
                }
                if(ptr->right != nullptr)
                {
                    queue.push(ptr->right);
                }
                TreeNode * tmp = ptr->left;
                ptr->left = ptr->right;
                ptr->right = tmp;
                queue.pop();
        }
        return root;
    }
};

101:对称二叉树

给你一个二叉树的根节点 root , 检查它是否轴对称。

class Solution {
public:
    TreeNode * fanzhuan(TreeNode * root)
    {
        
        queuequ1;
        if(root!=nullptr){qu1.push(root);}
        while(!qu1.empty())
        {
            TreeNode* ptr = qu1.front();
            qu1.pop();
            if(ptr->left != nullptr)
            {
                qu1.push(ptr->left);
            }
            if(ptr->right != nullptr)
            {
                qu1.push(ptr->right);
            }
            TreeNode* tmp = ptr->left;
            ptr->left = ptr->right;
            ptr->right = tmp;
        }
        return root;
    }
bool isSame(TreeNode* p, TreeNode* q) 
    {

	if (p == NULL && q == NULL) {
		return true;
	}
	if (p == NULL || q == NULL) {
		return false;
	}
	return p->val == q->val&&
		isSame(p->left, q->left) &&
		isSame(p->right, q->right);
    }
    bool isSymmetric(TreeNode* root) {
        fanzhuan(root->right);
        return isSame(root->left,root->right);
    }
};

104:二叉树的最大深度

给定一个二叉树,找出其最大深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int dep = 0;
        queuequeue;
        if(root!=nullptr){queue.push(root);}
        while(!queue.empty())
        {
            int size = queue.size();
            for(int i = 0;ileft != nullptr)
                {
                    queue.push(ptr->left);
                }
                if(ptr->right != nullptr)
                {
                    queue.push(ptr->right);
                }
            }
            dep++;
        }
        return dep;
    }
};

111.二叉树的最小深度

给定一个二叉树,找出其最小深度。

class Solution {
public:
    int minDepth(TreeNode* root) {
        int deap = 0;
        if(root == nullptr)
        {
            return deap;
        }
        queuequ;
        qu.push(root);
        deap += 1;
        while(!qu.empty())
        {
            int size = qu.size();
            for(int i = 0;ileft == nullptr && ptr->right == nullptr)
                {
                    return deap;
                }
                else
                {
                    if (ptr->left) qu.push(ptr->left);
                    if (ptr->right) qu.push(ptr->right);
                }
              }
            deap++;
        }
        return deap;
    }
};

222:完全二叉树节点个数

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == nullptr){return 0;}
        int sum = 0;
        queuequ;
        qu.push(root);
        while(!qu.empty())
        {
            int size = qu.size();
            for(int i = 0;ileft){qu.push(ptr->left);}
                if(ptr->right){qu.push(ptr->right);}
            }
        }
        return sum;
    }
};

110.平衡二叉树

给定一个二叉树,判断它是否是高度平衡的二叉树。

class Solution {
public:
     int getHeight(TreeNode *root)
     {
         if(root == nullptr) return 0;
         int leftHeight = getHeight(root->left);
         if (leftHeight == -1) return -1;
         int rightHeight = getHeight(root->right);
         if (rightHeight == -1) return -1;
         return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
     }
    bool isBalanced(TreeNode* root) {
        return (getHeight(root) == -1 ? false : true);
    }
};

257.二叉树的所有路径之和

给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。

class Solution {
public:
    void fun(TreeNode*root,vector&ar1,vector&ar2)
    {
        ar1.push_back(root->val);
        if(root->left == nullptr && root->right == nullptr) 
        {
            string str;
            for(int i = 0;ileft)
        {
            fun(root->left,ar1,ar2);
             ar1.pop_back();
        };
        if(root->right)
        {
            fun(root->right,ar1,ar2);
             ar1.pop_back();
        };
        return;
    }
    vector binaryTreePaths(TreeNode* root) {
        vectorar2;
        vectorar1;
        fun(root,ar1,ar2);
        return ar2;
    }
};

404.左叶子之和

给定二叉树的根节点 root ,返回所有左叶子之和。

class Solution {
public:

    int sumOfLeftLeaves(TreeNode* root) {
        if(root == nullptr) return 0;
        int sum1 = sumOfLeftLeaves(root->left);
        if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr)
        {
            sum1 = root->left->val;
        }
        int sum2 = sumOfLeftLeaves(root->right);
        
        return sum1+sum2;

    }
};

513.找树左下角的值

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queuequ;
        if(root == nullptr) return 0;
        qu.push(root);
        int result = 0;
        while(!qu.empty())
        {
            int size = qu.size()
;            for(int i = 0;ival;
                if(ptr->left != nullptr) qu.push(ptr -> left);
                if(ptr->right != nullptr)qu.push(ptr->right);
            }
        }
        return result;
    }
};

112.路经总和

class Solution {
public:
    bool travel(TreeNode *root,int sum)
    {
        if(!root->left && !root->right && sum == 0) return true;
        if(!root->left && !root->right) return false;
        if(root->left)
        {
            sum -= root->left->val;
            if(travel(root->left,sum)) return true;
            sum += root->left->val;
        }
        if(root->right)
        {
            sum -= root->right->val;
            if(travel(root->right,sum)) return true;
            sum += root->right->val;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr){return false;}
        return travel(root,targetSum-root->val);

    }
};

你可能感兴趣的:(算法,算法,leetcode)