5. leetcode(二叉树)

二叉树

//前序遍历

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if (root == NULL) {
            return res;
        }

        st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();
            st.pop();
            res.push_back(node->val);

            if (node->right) {
                st.push(node->right)
            }
            if (node->left) {
                st.push(node->left);
            }
        }
        return res;
    }    
};

//中序遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        TreeNode* cur = root;
        while (cur != nullptr || !st.empty()) 
        {
            if (cur != nullptr) {  // 指针访问节点,访问到最底层
                st.push(cur);      // 将访问的节点放入栈
                cur = cur->left;   // 左
            } else {               // 左空
                cur = st.top();    // 从栈里弹出数据
                st.pop();
                res.push_back(cur->val); //中
                cur = cur->right;  //右
            }
        }
        return res;
        
    }
};

二叉树的层序遍历

//层序遍历

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> que;

        if (root != nullptr) {
            que.push(root);
        }

        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;
            for (int i  = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                vec.push_back(node->val);

                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
            res.push_back(vec);
        }
        return res;
    }
};

1. leetcode 107 二叉树的层序遍历 II

/**
 * 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:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> que;
        if (root != nullptr) {
            que.push(root);
        }

        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;

            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                vec.push_back(node->val);

                if (node->left) {
                    que.push(node->left); 
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
            res.push_back(vec);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

2. leetcode 199 二叉树的右视图

/**
 * 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:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        queue<TreeNode*> que;

        if (root != nullptr) {
            que.push(root);
        }
        while (!que.empty()) {
            int size = que.size();

            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();

                if (i == size - 1) {   //将每一层的最后元素放入result数组中
                    res.push_back(node->val);
                }
                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return res;
    }
};

3. leetcode 637 二叉树的层平均值


/**
 * 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:
    vector<double> averageOfLevels(TreeNode* root) {
        vector<double> res;
        queue<TreeNode*> que;

        if (root != nullptr) {
            que.push(root); 
        }

        while (!que.empty()) {
            double sum = 0;
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();

                sum += node->val;

                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
            res.push_back(sum / size);  //将每一层的均指放入结果集
        }
        return res;
    }
};

4. leetcode 429. N 叉树的层序遍历

//4. leetcode 429. N 叉树的层序遍历
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> res;
        queue<Node*> que;

        if (root != nullptr) {
            que.push(root);
        }

        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;

            for (int i = 0; i < size; i++) {
                Node* node = que.front();
                que.pop();
                vec.push_back(node->val);
                //将孩子节点加入队列
                for (int i = 0; i < node->children.size(); i++) {  
                    if (node->children[i]) que.push(node->children[i]);
                }
            }
            res.push_back(vec);
        }
        return res;
    }
};

5. leetcode 515 在每个树行中找最大值

//5. leetcode 515 在每个树行中找最大值
/**
 * 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:
    vector<int> largestValues(TreeNode* root) {
        vector<int> res;
        queue<TreeNode*> que;
        if (root != nullptr) {
            que.push(root);
        }

        while (!que.empty()) {
            int size = que.size();
            int maxValue = INT_MIN;

            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                maxValue = node->val > maxValue ? node->val : maxValue;

                if (node->left) {
                    que.push(node->left); 
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
            res.push_back(maxValue);
        }
        return res;
    }
};

6. leetcode 116 填充每个节点的下一个右侧节点指针

//6.leetcode 116 填充每个节点的下一个右侧节点指针

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> que;

        if (root != nullptr) {
            que.push(root);
        }

        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;

            Node* nodepre;
            Node* node;
            for (int i = 0; i < size; i++) {
                if (i == 0) {
                    nodepre = que.front();  // 取出每层的头节点
                    que.pop();
                    node = nodepre;
                } else {
                    node = que.front();
                    que.pop();
                    nodepre->next = node;  //本层的前个节点的next值向本节点
                    nodepre = nodepre->next;
                }

                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
            nodepre->next = nullptr;   //本层的尾节点指向nullptr
        } 
        return root;
    }
};

7. leetcode 117 填充每个节点的下一个右侧节点指针 II

//7.leetcode 117 填充每个节点的下一个右侧节点指针 II

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> que;

        if (root != nullptr) {
            que.push(root);
        }

        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;

            Node* nodepre;
            Node* node;
            for (int i = 0; i < size; i++) {
                if (i == 0) {
                    nodepre = que.front();  // 取出每层的头节点
                    que.pop();
                    node = nodepre;
                } else {
                    node = que.front();
                    que.pop();
                    nodepre->next = node;  //本层的前个节点的next值向本节点
                    nodepre = nodepre->next;
                }

                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
            nodepre->next = nullptr;   //本层的尾节点指向nullptr
        } 
        return root;
    }
};

二叉树的属性

8. leetcode 226 翻转二叉树

//8.leetcode 226 翻转二叉树

/**
 * 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* invertTree(TreeNode* root) {
        if (root == nullptr) {
            return root;
        }

        std::swap(root->left, root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;

    }
};


//迭代 深度优先遍历

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) {
            return root;
        }
        stack<TreeNode*> st;
        st.push(root);

        while(!st.empty()) {
            TreeNode* node = st.top();
            st.pop();
            swap(node->left, node->right);
            if (node->left) {
                st.push(node->left);
            }
            if (node->right) {
                st.push(node->right);
            }
        }
        return root;
    }
};
//迭代 广度优先遍历
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != nullptr) {
            que.push(root);
        }
        while(!que.empty()) {
            int size = que.size();

            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                swap(node->left, node->right);
                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return root;
    }
};

9. leetcode 101 对称二叉树

//9.leetcode 101 对称二叉树 

//递归
/**
 * 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 compare(TreeNode* left, TreeNode* right) {
        //先排除空节点
        if (left == nullptr && right == nullptr) {
            return true;
        } else if (left != nullptr && right == nullptr) {
            return false;
        } else if (left == nullptr && right != nullptr) {
            return false;
        //排除数值不同的情况
        } else if (left->val != right->val){
            return false;
        }
        
        //左右不空且数值相同,做递归
        bool outside = compare(left->left, right->right);  //外
        bool inside = compare(left->right, right->left);   //内
        bool isSame = outside && inside;
        return isSame;
    }
    bool isSymmetric(TreeNode* root) {
        if (root == nullptr) {
            return true;
        }
        return compare(root->left, root->right);
    }
};
//迭代
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (root == nullptr) {
            return true;
        }
        queue<TreeNode*> que;
        que.push(root->left);  //左子树头节点加入队列
        que.push(root->right); //右子树头节点加入队列
        while (!que.empty()) {
            TreeNode* leftNode = que.front();
            que.pop();
            TreeNode* rightNode = que.front();
            que.pop();

            if (!leftNode && !rightNode) {  //左右节点都为空,则对称
                continue;
            }

            //左右节点有且仅有一个为空,或都不空但是数值不同,则false
            if (!leftNode || !rightNode || (leftNode->val != rightNode->val)) {
                return false;
            }

            que.push(leftNode->left);   // 加入左子树左孩子
            que.push(rightNode->right);
            que.push(leftNode->right);
            que.push(rightNode->left);

        }
        return true;
    }
};

10. leetcode 104 二叉树的最大深度

//10.leetcode 104 二叉树的最大深度

//递归
/**
 * 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 getDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int leftDepth = getDepth(root->left);
        int rightDepth = getDepth(root->right);
        int depth = 1 + max(leftDepth, rightDepth);
        return depth;
    }
    int maxDepth(TreeNode* root) {
        return getDepth(root);
    }
};

//迭代  层序遍历
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int depth = 0;
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if(node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return depth;
    }
};

11. leetcode 559 N 叉树的最大深度

//11.leetcode 559 N 叉树的最大深度

//递归
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    int maxDepth(Node* root) {
        if (root == nullptr) {
            return 0;
        }
        int depth = 0;

        for (int i = 0; i < root->children.size(); i++) {
            depth = max(depth, maxDepth(root->children[i]));
        }
        return depth + 1;
    }
};

//迭代

class Solution {
public:
    int maxDepth(Node* root) {
        queue<Node*> que;
        if (root != nullptr) {
            que.push(root);
        }

        int depth = 0;
        while (!que.empty()) {
            int size = que.size();
            depth++;
            for (int i = 0; i < size; i++) {
                Node* node = que.front();
                que.pop();
                for (int j = 0; j < node->children.size(); j++) {
                    if (node->children[j]) {
                        que.push(node->children[j]);
                    }
                }
            }
        }
        return depth;
    }
};

12. leetcode 111 二叉树的最小深度

//12.leetcode 111 二叉树的最小深度

//递归
/**
 * 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 getDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }

        int leftDepth = getDepth(root->left);
        int rightDepth = getDepth(root->right);

        //当一个左子树为空,右不为空,这是并不是最低点
        if (root->left == nullptr && root->right != nullptr) {
            return 1 + rightDepth;
        }
        if (root->left != nullptr && root->right == nullptr) {
            return 1 + leftDepth;
        }
        int res = 1 + min(leftDepth, rightDepth);
        return res;
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

//迭代
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int depth = 0;
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();

                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
                if (!node->left && !node->right) {
                    return depth;
                }
            }
        }
        return depth;
    }
};

class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == nullptr) {
            return 0;
        }

        if (root->left == nullptr && root->right == nullptr) {
            return 1;
        }

        int min_depth = INT_MAX;
        if (root->left != nullptr) {
            min_depth = min(minDepth(root->left), min_depth);
        }
        if (root->right != nullptr) {
            min_depth = min(minDepth(root->right), min_depth);
        }

        return min_depth + 1;
    }
};

13. leetcode 222 完全二叉树的节点个数

//13.leetcode 222 完全二叉树的节点个数

// 按普通二叉树来解
//递归
/**
 * 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 getNodeNum(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int leftNum = getNodeNum(root->left);
        int rightNum = getNodeNum(root->right);
        int nodenum = 1 + leftNum + rightNum;
        return nodenum;
    }
    int countNodes(TreeNode* root) {
        return getNodeNum(root);
    }
};

//迭代
class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != nullptr) {
            que.push(root);
        }
        int res = 0;
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                res++;
                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return res;
    }
};

//利用完全二叉树性质

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        TreeNode* leftnode = root->left;
        TreeNode* rightnode = root->right;
        int leftheight = 0, rightheight = 0;
        //求左子树深度
        while (leftnode) {   
            leftnode = leftnode->left;
            leftheight++;
        }
        //求右子树深度
        while(rightnode) {
            rightnode = rightnode->right;
            rightheight++;
        }
        if (leftheight == rightheight) {
            return (2 << leftheight) - 1;  //(2 << 1) 相当于 2^2 所以leftheight初始为0
        }
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};

14. leetcode 110 平衡二叉树

//14.leetcode 110 平衡二叉树

//递归
/**
 * 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 getDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int leftDepth = getDepth(root->left);
        if (leftDepth == -1) {  //说明左子树已经不是二叉平衡树
            return -1;
        }
        int rightDepth = getDepth(root->right);
        if (rightDepth == -1) {  // 说明右子树已经不是二叉平衡树
            return -1;
        }
        return abs(leftDepth - rightDepth) > 1 ? -1 :1 + max(leftDepth, rightDepth);
    }
    bool isBalanced(TreeNode* root) {
        return getDepth(root) == -1 ? false : true;
    }
};

15. leetcode 257 二叉树的所有路径

//15.leetcode 257 二叉树的所有路径

//深度优先搜索
/**
 * 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 construct_paths(TreeNode* root, string path, vector<string>& paths) {
        if (root != nullptr) {
            path += to_string(root->val);
            //当前节点是叶子节点 把路径加入到答案中
            if (root->left == nullptr && root->right == nullptr) {
                paths.push_back(path);
            }  else {
                path += "->";   //当前节点不是叶子节点,继续递归遍历
                construct_paths(root->left, path, paths);
                construct_paths(root->right, path, paths);
            }
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> paths;
        construct_paths(root, "", paths);
        return paths;
    }
};

16. leetcode 100 相同的树

//16.leetcode 100 相同的树

//递归  深度优先搜索
/**
 * 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 isSameTree(TreeNode* p, TreeNode* q) {
        if (p == nullptr && q == nullptr) {
            return true;
        } else if (p == nullptr || q == nullptr) {
            return false;
        } else if (p->val != q->val) {
            return false;
        } else {
            return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
    }
};

17. leetcode 572 另一棵树的子树

//17.leetcode 572 另一棵树的子树
class Solution {
public:
    bool isSame(TreeNode* root, TreeNode* subRoot) {
        if (!root && !subRoot) {
            return true;
        }
        if (root->val != subRoot->val) {
            return false;
        }
        return isSame(root->left, subRoot->left) && isSame(root->right, subRoot->right);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        if (!root) {
            return false;
        }
        if (isSame(root, subRoot)) {
            return true;
        }
        return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
    }
    
};

18. leetcode 404 左叶子之和

//18.leetcode 404 左叶子之和

/**
 * 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;
        }

        int leftValue = sumOfLeftLeaves(root->left);
        int rightValue = sumOfLeftLeaves(root->right);
        int midValue = 0;
        if (root->left && !root->left->left && !root->left->right) {
            midValue = root->left->val;
        }
        int sum = midValue + leftValue + rightValue;
        return sum;
    }
};

19. leetcode 513 找树左下角的值

//19.leetcode 513 找树左下角的值

/**
 * 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;
        if (root != nullptr) {
            que.push(root);
        }
        int res = 0;
        while(!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (i == 0) {
                    res = node->val;  //每行的首元素
                }
                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return res;
    }
};

20. leetcode 112 路径总和

//20.leetcode 112 路径总和
class solution {
private:
    bool traversal(treenode* cur, int count) {
        if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0
        if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回

        if (cur->left) { // 左
            count -= cur->left->val; // 递归,处理节点;
            if (traversal(cur->left, count)) return true;
            count += cur->left->val; // 回溯,撤销处理结果
        }
        if (cur->right) { // 右
            count -= cur->right->val; // 递归,处理节点;
            if (traversal(cur->right, count)) return true;
            count += cur->right->val; // 回溯,撤销处理结果
        }
        return false;
    }

public:
    bool haspathsum(treenode* root, int sum) {
        if (root == null) return false;
        return traversal(root, sum - root->val);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值