Code Random Notes--Binary Tree

1-- recursive traversal

1-1--Preorder traversal

Preorder traversal: root → left → right;

#include <iostream>
#include <vector>

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:
    std::vector<int> preorderTraversal(TreeNode* root) {
        std::vector<int> res;
        dfs(root, res);
        return res;
    }

    void dfs(TreeNode* root, std::vector<int>& res){
        if(root == nullptr) return;
        res.push_back(root->val);
        dfs(root->left, res);
        dfs(root->right, res);
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    Node1->right = Node2;
    Node2->left = Node3;

    Solution S1;
    std::vector<int> res = S1.preorderTraversal(Node1);
    for(auto item : res) std::cout << item << " ";
    std::cout << std::endl;
    return 0;
}

1-2--Inorder traversal

Inorder traversal: left → root → right;

#include <iostream>
#include <vector>

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:
    std::vector<int> inorderTraversal(TreeNode* root) {
        std::vector<int> res;
        dfs(root, res);
        return res;
    }

    void dfs(TreeNode* root, std::vector<int>& res){
        if(root == nullptr) return;
        dfs(root->left, res);
        res.push_back(root->val);
        dfs(root->right, res);
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    Node1->right = Node2;
    Node2->left = Node3;

    Solution S1;
    std::vector<int> res = S1.inorderTraversal(Node1);
    for(auto item : res) std::cout << item << " ";
    std::cout << std::endl;
    return 0;
}

1-3--post-order traversal

Post-order traversal: left → right → root;

#include <iostream>
#include <vector>

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:
    std::vector<int> postorderTraversal(TreeNode* root) {
        std::vector<int> res;
        dfs(root, res);
        return res;
    }

    void dfs(TreeNode* root, std::vector<int>& res){
        if(root == nullptr) return;
        dfs(root->left, res);
        dfs(root->right, res);
        res.push_back(root->val);
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    Node1->right = Node2;
    Node2->left = Node3;

    Solution S1;
    std::vector<int> res = S1.postorderTraversal(Node1);
    for(auto item : res) std::cout << item << " ";
    std::cout << std::endl;
    return 0;
}

2--Iterative traversal

2-1--Preorder traversal

        Based on the stack structure, the root node is first pushed into the stack, and then the node is popped from the stack. If the right child of the node is not empty, the right child is pushed into the stack; if the left child of the node is not empty, the left child is pushed into the stack;

        Circulate the processing nodes out of the stack, and store the right child and the left child in the stack (the right child is first stacked, and the left child is pushed into the stack, because the stack is first in and then out, so it can ensure that the left child is out of the stack first, in line with the root → left → right order);

#include <iostream>
#include <vector>
#include <stack>

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:
    std::vector<int> preorderTraversal(TreeNode* root) {
        std::vector<int> res;
        if(root == nullptr) return res;
        std::stack<TreeNode*> stk;
        stk.push(root);
        while(!stk.empty()){
            TreeNode *tmp = stk.top();
            stk.pop();
            res.push_back(tmp->val);
            if(tmp->right != nullptr) stk.push(tmp->right); // 右
            if(tmp->left != nullptr) stk.push(tmp->left); // 左
        }
        return res;
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    Node1->right = Node2;
    Node2->left = Node3;

    Solution S1;
    std::vector<int> res = S1.preorderTraversal(Node1);
    for(auto item : res) std::cout << item << " ";
    std::cout << std::endl;
    return 0;
}

2-2--post-order traversal

        It can be implemented using two stacks, one is to traverse the stack and the other is to collect the stack. Refer to the previous notes: Iterative implementation of post-order traversal        

        It can also be similar to pre-order traversal, based on a stack implementation, but the stacking order needs to be changed: every time a node is popped out of the stack, its left child is pushed into the stack, and then the right child is pushed into the stack; at this time, the processing sequence is: root -> right -> Left, and finally reverse the result;

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

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:
    std::vector<int> postorderTraversal(TreeNode* root) {
        std::vector<int> res;
        if(root == nullptr) return res;
        std::stack<TreeNode*> stk;
        stk.push(root);
        while(!stk.empty()){
            TreeNode* tmp = stk.top();
            stk.pop();
            if(tmp->left != nullptr) stk.push(tmp->left);
            if(tmp->right != nullptr) stk.push(tmp->right);
            res.push_back(tmp->val);
        }
        // 反转
        std::reverse(res.begin(), res.end());
        return res;
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    Node1->right = Node2;
    Node2->left = Node3;

    Solution S1;
    std::vector<int> res = S1.postorderTraversal(Node1);
    for(auto item : res) std::cout << item << " ";
    std::cout << std::endl;
    return 0;
}

2-3--Inorder traversal

Based on the stack structure, initialize a stack and push the root node into the stack;

        ①: All the left child nodes are pushed into the stack;

        ②: The node is popped out of the stack, and the node is processed;

        ③: Repeat step ① for the right subtree of the popped node;

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

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:
    std::vector<int> inorderTraversal(TreeNode* root) {
        std::vector<int> res;
        if(root == nullptr) return res;
        std::stack<TreeNode*> stk;
        while(!stk.empty() || root != nullptr){
            if(root != nullptr){ // 左子结点全部入栈
                stk.push(root);
                root = root->left;
            }
            else{
                TreeNode *tmp = stk.top();
                stk.pop();
                res.push_back(tmp->val);
                // 出栈节点的右孩子执行相同操作
                root = tmp->right;
            }    
        }
        return res;
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    Node1->right = Node2;
    Node2->left = Node3;

    Solution S1;
    std::vector<int> res = S1.inorderTraversal(Node1);
    for(auto item : res) std::cout << item << " ";
    std::cout << std::endl;
    return 0;
}

3--Level order traversal of binary tree

Main ideas:

        Classical breadth-first search, based on queues;

        For this question, the nodes of the same layer need to be placed in an array, so when traversing, a variable nums needs to be used to record the number of nodes in the current layer, that is, nums is equal to the number of queue elements;

#include <iostream>
#include <vector>
#include <queue>

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:
    std::vector<std::vector<int>> levelOrder(TreeNode* root) {
        std::vector<std::vector<int>> res;
        if(root == nullptr) return res;
        std::queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            int nums = q.size(); // 当前层的节点数
            std::vector<int> tmp;
            while(nums > 0){ // 遍历处理同一层
                TreeNode *cur = q.front();
                q.pop();
                tmp.push_back(cur->val);

                if(cur->left != nullptr) q.push(cur->left);
                if(cur->right != nullptr) q.push(cur->right);
                
                nums--;
            }
            res.push_back(tmp); // 记录当前层的元素
        }
        return res;
    }
};

int main(int argc, char* argv[]){
    // root = [1, null, 2, 3]
    TreeNode *Node1 = new TreeNode(3);
    TreeNode *Node2 = new TreeNode(9);
    TreeNode *Node3 = new TreeNode(20);
    TreeNode *Node4 = new TreeNode(15);
    TreeNode *Node5 = new TreeNode(7);
    Node1->left = Node2;
    Node1->right = Node3;
    Node3->left = Node4;
    Node3->right = Node5;

    Solution S1;
    std::vector<std::vector<int>> res = S1.levelOrder(Node1);
    for(auto item : res) {
        for (int v : item) std::cout << v << " ";
        std::cout << std::endl;
    }
    return 0;
}

4-- Flip the binary tree

Main ideas:

        Recursively swap left and right subtrees;

#include <iostream>
#include <vector>
#include <queue>

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) {
        reverse(root);
        return root;
    }
    void reverse(TreeNode *root){
        if(root == nullptr) return;
        reverse(root->left);
        reverse(root->right);
        TreeNode *tmp = root->left;
        root->left = root->right;
        root->right = tmp;
    }
};

// 层次遍历打印
void PrintTree(TreeNode *root){
    std::queue<TreeNode*> q;
    q.push(root);
    while(!q.empty()) {
        TreeNode *tmp = q.front();
        q.pop();
        std::cout << tmp->val << " ";
        if(tmp->left != nullptr) q.push(tmp->left);
        if(tmp->right != nullptr) q.push(tmp->right);
    }
}

int main(int argc, char* argv[]){
    // root = [4,2,7,1,3,6,9]
    TreeNode *Node1 = new TreeNode(4);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(7);
    TreeNode *Node4 = new TreeNode(1);
    TreeNode *Node5 = new TreeNode(3);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(9);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    TreeNode *res = S1.invertTree(Node1);
    PrintTree(res);
}

5--Symmetric binary tree

Main ideas:

        Recursively judge whether the left subtree of the left tree is equal to the right subtree of the right number, and whether the right subtree of the left tree is equal to the left subtree of the right tree;

#include <iostream>
#include <vector>
#include <queue>

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 isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        bool res = dfs(root->left, root->right);
        return res;
    }
    bool dfs(TreeNode *left, TreeNode *right){
        if((left != nullptr && right == nullptr) ||
        (left == nullptr && right != nullptr)) return false;

        if(left == nullptr && right == nullptr) return true;
        if (left->val != right->val) return false;

        bool isSame1 = dfs(left->left, right->right);
        bool isSame2 = dfs(left->right, right->left);
        return isSame1 && isSame2;
    }
};

int main(int argc, char* argv[]){
    // root = [4,2,7,1,3,6,9]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(2);
    TreeNode *Node4 = new TreeNode(3);
    TreeNode *Node5 = new TreeNode(4);
    TreeNode *Node6 = new TreeNode(4);
    TreeNode *Node7 = new TreeNode(3);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    bool res = S1.isSymmetric(Node1);
    if(res) std::cout << "true" << std::endl;
    else std::cout << "false" << std::endl;
}

6--The maximum depth of the binary tree

Main ideas:

        Recursively calculate the depth of the left and right subtrees, and select the maximum value of the two + 1 to return;

#include <iostream>
#include <vector>
#include <queue>

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 maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        int res = dfs(root);
        return res;
    }

    int dfs(TreeNode* root){
        if(root == nullptr) return 0;
        int left_height = dfs(root->left);
        int right_height = dfs(root->right);
        int cur_height = std::max(left_height, right_height) + 1;
        return cur_height;
    }
};

int main(int argc, char* argv[]){
    // root = [3,9,20,null,null,15,7]
    TreeNode *Node1 = new TreeNode(3);
    TreeNode *Node2 = new TreeNode(9);
    TreeNode *Node3 = new TreeNode(20);
    TreeNode *Node4 = new TreeNode(15);
    TreeNode *Node5 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node3->left = Node4;
    Node3->right = Node5;

    Solution S1;
    int res = S1.maxDepth(Node1);
    std::cout << res << std::endl;
    return 0;
}

7--The minimum depth of the binary tree

Main ideas:

        Similar to the previous question, it is enough to return the minimum depth recursively, but it is necessary to eliminate the case where a subtree of the root node is empty;

        For a root node, one of its subtrees is empty, its minimum depth is the depth of the non-empty subtree;

#include <iostream>
#include <vector>
#include <queue>

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

    int dfs(TreeNode *root){
        if(root == nullptr) return 0;
        // 剔除两种情况
        if(root->left == nullptr) return dfs(root->right) + 1;
        else if(root->right == nullptr) return dfs(root->left) + 1;
        else{
            int left_height = dfs(root->left);
            int right_height = dfs(root->right);
            int cur_min_height = std::min(left_height, right_height) + 1;
            return cur_min_height;
        }
    }
};

int main(int argc, char* argv[]){
    // root = [3,9,20,null,null,15,7]
    TreeNode *Node1 = new TreeNode(3);
    TreeNode *Node2 = new TreeNode(9);
    TreeNode *Node3 = new TreeNode(20);
    TreeNode *Node4 = new TreeNode(15);
    TreeNode *Node5 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node3->left = Node4;
    Node3->right = Node5;

    Solution S1;
    int res = S1.minDepth(Node1);
    std::cout << res << std::endl;
    return 0;
}

8--The number of complete binary tree nodes

Main ideas:

        Ordinary binary trees can count the number of nodes through hierarchical traversal;

        For the complete binary tree in this question, the number of binary tree nodes can be calculated by the formula 2**k - 1;

        First of all, it is necessary to judge whether a subtree is a complete binary tree, and if so, calculate it through the above formula; if it is not a complete binary tree, then for the current subtree, it is necessary to recursively calculate the number of nodes in the left and right subtrees (equivalent to obtaining information), and finally Add the results (equivalent to processing information), and add 1 to return;

#include <iostream>
#include <vector>
#include <queue>

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 countNodes(TreeNode* root) {
        if(root == nullptr) return 0;
        return dfs(root);
    }
    int dfs(TreeNode *root){
        if(root == nullptr) return 0;
        TreeNode *left = root->left, *right = root->right;
        int left_height = 0, right_height = 0;
        while(left != nullptr){
            left = left->left;
            left_height++;
        }
        while(right != nullptr){
            right = right->right;
            right_height++;
        }
        if(left_height == right_height) return (2<<left_height) - 1; // 满二叉树
        int left_nums = dfs(root->left);
        int right_nums = dfs(root->right);
        return left_nums + right_nums + 1;
    }
};

int main(int argc, char* argv[]){
    // root = [1,2,3,4,5,6]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;

    Solution S1;
    int res = S1.countNodes(Node1);
    std::cout << res << std::endl;
    return 0;
}

9--Balanced binary tree

Main ideas:

Recursively judge whether the subtree is a balanced binary tree         by height difference not greater than 1 , if not, return -1, if yes, return the corresponding height;

#include <iostream>
#include <vector>

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 isBalanced(TreeNode* root) {
        if(root == nullptr) return true;
        int height = dfs(root);
        return height == -1 ? false : true;
    }

    int dfs(TreeNode *root){
        if(root == nullptr) return 0;
        int left_height = dfs(root->left);
        if(left_height == -1) return -1;
        int right_height = dfs(root->right);
        if(right_height == -1) return -1;
        if(std::abs(left_height - right_height) > 1) return -1;
        else return std::max(left_height, right_height) + 1;
    }
};

int main(int argc, char* argv[]){
    // root = [3,9,20,null,null,15,7]
    TreeNode *Node1 = new TreeNode(3);
    TreeNode *Node2 = new TreeNode(9);
    TreeNode *Node3 = new TreeNode(20);
    TreeNode *Node4 = new TreeNode(15);
    TreeNode *Node5 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node3->left = Node4;
    Node3->right = Node5;

    Solution S1;
    bool res = S1.isBalanced(Node1);
    if(res) std::cout << "true" << std::endl;
    else std::cout << "false" << std::endl;
    return 0;
}

10--All paths of the binary tree

Main ideas:

        recursive record path;

#include <iostream>
#include <vector>
#include <string>

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:
    std::vector<std::string> binaryTreePaths(TreeNode* root) {
        std::vector<std::string> res;
        if(root == nullptr) return res;
        std::string path = "";
        dfs(root, res, path);
        return res;
    }

    void dfs(TreeNode *root, std::vector<std::string>& res, std::string path){
        if(root == nullptr) return;

        path += std::to_string(root->val);
        if(root->left == nullptr && root->right == nullptr) { // 叶子节点,回收路径
            res.push_back(path);
            return;
        }
        else path += "->";
        dfs(root->left, res, path);
        dfs(root->right, res, path);
    }
};

int main(int argc, char* argv[]){
    // root = [1,2,3,null,5]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(5);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->right = Node4;

    Solution S1;
    std::vector<std::string> res = S1.binaryTreePaths(Node1);
    for(auto path : res) std::cout << path << std::endl;
    return 0;
}

11--Sum of left leaves

Main ideas:

        Recurse to the upper layer of the leaf node and return the sum of its left leaves;

#include <iostream>
#include <vector>
#include <string>

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;
        return dfs(root);
    }
    int dfs(TreeNode* root){
        if(root == nullptr) return 0;
        if(root->left == nullptr && root->right == nullptr) return 0;
        int sum = 0;
        if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr){
            sum = root->left->val;
        }
        int left = dfs(root->left);
        int right = dfs(root->right);
        return left + right + sum;
    }
};

int main(int argc, char* argv[]){
    // root = [3,9,20,null,null,15,7]
    TreeNode *Node1 = new TreeNode(3);
    TreeNode *Node2 = new TreeNode(9);
    TreeNode *Node3 = new TreeNode(20);
    TreeNode *Node4 = new TreeNode(15);
    TreeNode *Node5 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node3->left = Node4;
    Node3->right = Node5;

    Solution S1;
    int res = S1.sumOfLeftLeaves(Node1);
    std::cout << res << std::endl;
    return 0;
}

12--Find the value in the lower left corner of the tree

Main ideas:

        When recursing to the maximum depth layer, the leftmost node value is returned first, that is, the left subtree is searched first when recursing;

#include <iostream>
#include <vector>
#include <limits.h>

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) {
        if(root == nullptr) return 0;
        int max_height = INT_MIN;
        int result = 0;
        dfs(root, 0, max_height, result);
        return result;
    }

    void dfs(TreeNode* root, int curheight, int& max_height, int& res){
        if(root == nullptr) return;
        if(root->left == nullptr && root->right == nullptr){ // 叶子节点
            if(curheight + 1 > max_height){
                max_height = curheight + 1;
                res = root->val;
                return;
            }
        }
        dfs(root->left, curheight+1, max_height, res);
        dfs(root->right, curheight+1, max_height, res);  
    }
};

int main(int argc, char* argv[]){
    // root = [3,9,20,null,null,15,7]
    TreeNode *Node1 = new TreeNode(2);
    TreeNode *Node2 = new TreeNode(1);
    TreeNode *Node3 = new TreeNode(3);

    Node1->left = Node2;
    Node1->right = Node3;

    Solution S1;
    int res = S1.findBottomLeftValue(Node1);
    std::cout << res << std::endl;
    return 0;
}

13--path sum

Guess you like

Origin blog.csdn.net/weixin_43863869/article/details/132636939