【Leetcode_Explore】Binary Tree

Binary Tree Preorder Traversal

迭代

/**
 * 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> result;
    vector<int> temp;
    vector<int> preorderTraversal(TreeNode* root){//leetcode只认相同输入参数的函数
        return preorderTraversal_help(root, result);
    }
    vector<int> preorderTraversal_help(TreeNode* root, vector<int>& result) {
        if(root == NULL) return temp;//leetcode要求不能返回空
        result.push_back(root->val);
        preorderTraversal_help(root->left, result);
        preorderTraversal_help(root->right, result);
        return result;
    }
};

非迭代(注意栈里压入的须是非空指针)

    vector<int> result;
    vector<int> preorderTraversal(TreeNode* root){
        if(root == NULL)//这里必须要考虑root==NULL,压入空指针下面的val就会出错
            return result;
        stack<TreeNode*> sta;
        sta.push(root);
        while(!sta.empty()){//空指针也会使得栈非空
            TreeNode *top_one = sta.top();
            result.push_back(top_one->val);//
            sta.pop();
            if(top_one->right != NULL)//保证压入的不是空指针
                sta.push(top_one->right);
            if(top_one->left != NULL)
                sta.push(top_one->left);
        };
        return result;
    }

中序遍历不再说了

重点再说下使用非迭代实现后序遍历,这里需要用到两个栈,用来处理最后才遍历到root结点的问题

/**
 * 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> temp;//leetcode需要返回的没有用的
    vector<int> result_vector;//最终返回的
    stack<TreeNode*> sta;//保存需要先后处理的  
    stack<int> result;//临时的倒着放的result
    vector<int> postorderTraversal(TreeNode* root) {
        if(root == NULL) return temp;
        sta.push(root);
        while(!sta.empty()){
            TreeNode *top_one = sta.top();
            result.push(top_one->val);
            sta.pop();
            if(top_one->left != NULL)
                sta.push(top_one->left);//先放进去的在sta的下面
            if(top_one->right != NULL)//后放进去的在sta的上面,但是val值压在了result的下面
                sta.push(top_one->right);
            
        }
        while(!result.empty()){
            result_vector.push_back(result.top());
            result.pop();
        }
        return result_vector;
    }
};

参考:https://blog.csdn.net/weixin_40087851/article/details/81947783

层遍历,使用队列。如果不要求分层输出,而是整体直接输出,会简单很多,不用计数

/**
 * 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>> result;
        queue<TreeNode*> que;
        vector<int> temp;
        int current_num = 1;
        int next_num = 0;
        if(!root) return result;
        que.push(root);
        while(!que.empty()){
            TreeNode* front = que.front();
            que.pop();
            temp.push_back(front->val);
            current_num--;
            if(front->left){
                que.push(front->left);
                next_num++;}
            if(front->right){
                que.push(front->right);
                next_num++;}
            if(current_num == 0){
                result.push_back(temp);
                current_num = next_num;
                temp.clear();
                next_num = 0;//必须要有,这里完成交接,对下一层重新开始计数
            }
        }
        return result;
    }
};

Recursion is one of the nature features of a tree.

Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
     / \
    4   8
   /   / \
  11  13  4
 /  \      \
7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

原答案

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(!root->left && !root->right) return root->val == sum;
        if(!root->left)
            return hasPathSum(root->right, sum-root->val);
        else if(!root->right)
            return hasPathSum(root->left, sum-root->val);
        return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
    }    
};

一开始root==NULL,出错

修改版

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(!root) return false;//终止条件,剪枝
        if(!root->left && !root->right) return root->val == sum;//终止,叶子结点
        return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
    }    
};

猜你喜欢

转载自blog.csdn.net/weixin_39458342/article/details/88757996