• 前序遍历

前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树

 

task5-二叉树

使用递归:

 /**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int>res;
    vector<int> preorderTraversal(TreeNode* root) {
        if(root)
        {
            res.push_back(root->val);
            preorderTraversal(root->left);
            preorderTraversal(root->right);
        }
        return res;
        
    }
};

  • 中序遍历

中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。

task5-二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int>res;
    vector<int> inorderTraversal(TreeNode* root) {
        if(root)
        {
            inorderTraversal(root->left);
            res.push_back(root->val);
            inorderTraversal(root->right);
        }
        return res;
    }
}; 

  • 后序遍历

先遍历左子树,然后遍历右子树,最后访问根节点 

task5-二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int>res;
    vector<int> postorderTraversal(TreeNode* root) {
        if(root)
        {
            postorderTraversal(root->left);
            postorderTraversal(root->right);
            res.push_back(root->val);
        }
        return res;
    }
}; 

 层序遍历

层序遍历就是逐层遍历树结构

 

task5-二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        
        vector<vector<int>> res;
        if(root)
        {
        vector<int>res2;
        res2.push_back(root->val);
        res.push_back(res2);
        vector<TreeNode*> node;
        node.push_back(root);
        int last=1;
        int accur=0;
        while(accur<node.size())
        {
            last=node.size();
          
            vector<int> res1;
            int res3=0;
            while(accur<last)
            {
               
                if(node[accur]->left)
                { 
                    node.push_back(node[accur]->left);
                    res1.push_back(node[accur]->left->val);
                    res3++;
                }
                if(node[accur]->right)
                {
                    node.push_back(node[accur]->right);
                    res1.push_back(node[accur]->right->val);
                    res3++;
                }
                 
                accur++;
            }
            if(res3!=0)
            res.push_back(res1);
        }
        }
        return res;
    }
}; 

  • 二叉树的最大深度

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        return root ? max(maxDepth(root->left), maxDepth(root->right)) + 1 : 0; 
    }
};

  •  路径之和

 /**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root==NULL)
        {
            return false;
        }
        int t= sum-root->val;
        if(root->left==NULL && root->right==NULL)
            return t==0 ? true:false;
        return hasPathSum(root->left,t) || hasPathSum(root->right,t);
    }
};

 

相关文章:

  • 2022-12-23
  • 2021-08-14
  • 2021-06-29
  • 2022-12-23
  • 2021-12-21
  • 2021-07-30
猜你喜欢
  • 2021-07-07
  • 2021-07-14
  • 2021-12-19
  • 2022-12-23
  • 2022-01-01
  • 2021-10-31
  • 2021-05-28
相关资源
相似解决方案