寒假刷题打卡第十二天 | 树

  1. 找树左下角的值
    BFS.
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> q;
        int curLevel = 0;
        int nextLevel = 0;
        q.push(root);
        TreeNode* tempNode;
        while(q.size())
        {
            tempNode = q.front();
            q.pop();
            curLevel--;//事实上,这道题中并不需要维护这个东西...
            if(tempNode->right)
            {
                q.push(tempNode->right);
                nextLevel++;
            }
            if(tempNode->left)
            {
                q.push(tempNode->left);
                nextLevel++;
            }
            if(!curLevel)
                curLevel = nextLevel;
        }
        return tempNode->val;
    }
};

DFS.
为啥DFS比bfs快很多?

class Solution {
public:
    int maxLevel = 0;
    int res;
    int findBottomLeftValue(TreeNode* root) {
        res = root->val;
        dfs(root, 0);
        return res;
    }
    void dfs(TreeNode* root, int level)
    {
        if(level > maxLevel)
        {
            res = root->val;
            maxLevel = level;
        }
        if(root->left)
            dfs(root->left, level+1);
        if(root->right)
            dfs(root->right, level+1);
    }
};
  1. 非递归实现二叉树的前序遍历
    by stack. 递归是一个由操作系统完成的一个隐式栈,迭代就是把它显式化。所以,如果是把一个通常递归的方法也是可以借助栈通过迭代来完成的。
class Solution {
public:
    vector<int> ans;
    vector<int> preorderTraversal(TreeNode* root) {
        if(!root)
            return ans;
        stack<TreeNode*> s;
        s.push(root);
        while(s.size())
        {
            TreeNode* tempNode = s.top();
            ans.push_back(tempNode->val);
            s.pop();
            if(tempNode->right)
                s.push(tempNode->right);
            if(tempNode->left)
                s.push(tempNode->left);
            
        }
        return ans;
    }
};
  1. 非递归后序遍历
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> ans;
        if(!root)
            return ans;
        stack<TreeNode*> s;
        TreeNode* prev=nullptr;
        while(root || s.size())
        {
            while(root)
            {
                s.push(root);
                root = root->left;
            }
            root = s.top();
            s.pop();
            if(!root->right || root->right == prev) //如果右边为空或者已经访问过
            {
                ans.push_back(root->val);
                prev = root;
                root = nullptr;
            }
            else
            {
                s.push(root);
                root = root->right;
            }
        }
        return ans;
    }
};
  1. 二叉树的中序遍历
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        if(!root)
            return ans;
        stack<TreeNode*> s;
        while(root || s.size())
        {
            while(root)
            {
                s.push(root);
                root = root->left;
            }
            root = s.top();
            s.pop();
            ans.push_back(root->val);
            if(root->right)
                root = root->right;
            else
                root = nullptr;
        }
        return ans;
    }
};
  1. 修剪二叉搜索树
    二叉搜索树的每一个根节点都大于等于左子树的节点,小于等于右子树的每一个节点。
    递归。
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(!root)
            return nullptr;
        if(root->val >= low && root->val<=high)
        {
            root->left = trimBST(root->left, low, high);
            root->right = trimBST(root->right, low, high);
            return root;
        }
        if(root->val<low)
            return trimBST(root->right, low, high);
        else
            return trimBST(root->left, low, high);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值