• leetcode 树


    1.binary-tree-postorder-traversal

    解答:输出树后序遍历的结果(非递归)。采用先序遍历(把根左右改为根右左),然后reverse一下。

    //树的非递归前序遍历(入栈时先压入右子树再压入左子树)
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode *root) {
            vector<int> res;
            if(!root)return res;
            stack<TreeNode *> st;
            st.push(root);
            while(st.size()){
                TreeNode *pNode = st.top();
                st.pop();
                res.push_back(pNode->val);
                if(pNode->right)st.push(pNode->right);
                if(pNode->left)st.push(pNode->left);
            }
            return res;
        }
    };
    //非递归的后序遍历(先压入左子树再压入右子树,最后reverse一下,中右左->左右中)
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode *root) {
            vector<int> res;
            if(!root)
                return res;
            stack<TreeNode *> st;
            st.push(root);
            while(st.size()){
                TreeNode *p = st.top();
                st.pop();
                res.push_back(p->val);
                if(p->left)
                    st.push(p->left);
                if(p->right)
                    st.push(p->right);
               
            }
            reverse(res.begin(),res.end());
            return res;
        }
    };

    //树的非递归中序遍历(不断把左孩子入栈,每次弹出时检测是否有右孩子有则入栈)
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode *root) {
            vector<int> res;
            if(!root)return res;
            stack<TreeNode *> st;
            TreeNode *pNode = root;
            while(st.size()||pNode){
                while(pNode){
                    st.push(pNode);
                    pNode = pNode->left;
                }
                pNode = st.top();
                st.pop();
                res.push_back(pNode->val);
                pNode = pNode->right;
               
            }
            return res;
        }
    };

    2. sum-root-to-leaf-numbers

    解答:递归结合先序遍历

    class Solution {
    public:
        int sumNumbers(TreeNode *root) {
            if(!root)return 0;
            int sum=0;
            return preOrder(root,sum);
        }
        int preOrder(TreeNode *root, int sum){
            if(!root)return 0;
            sum = sum*10+root->val;
            if((!root->left)&&(!root->right))return sum;
            else return preOrder(root->left,sum)+preOrder(root->right,sum);
        }
    };

     3. binary-tree-maximum-path-sum

    依旧是递归的思想,这里加入一个全局变量。

    class Solution {
    public:
        int Max;
            int maxPathSum(TreeNode *root) {
                Max = INT_MIN;
                maxSum(root);
                return Max;
            }
            int maxSum(TreeNode *root) {
                if(root == NULL)
                    return 0;
                int l_Max = max(0, maxSum(root->left));
                int r_Max = max(0, maxSum(root->right));
                Max = max(Max, l_Max + r_Max + root->val);
                return max(l_Max, r_Max) + root->val;
            }
    };

    4.populating-next-right-pointers-in-each-node

    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if(!root)return;
            if(root->left&&root->right)
                root->left->next = root->right;
            if(root->next&&root->right)
                root->right->next = root->next->left;
            connect(root->left);
            connect(root->right);
        }
    };

    5.populating-next-right-pointers-in-each-node-ii

    解答:层次遍历,依次遍历每一层。

    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if(!root)return;
            queue<TreeLinkNode *> cur;
            cur.push(root);
            while(cur.size()){
                int len = cur.size();
                for(int i=0;i<len;i++){
                    TreeLinkNode *p = cur.front();
                    cur.pop();
                    if(p->left)cur.push(p->left);
                    if(p->right)cur.push(p->right);
                    if(i==len-1)p->next=nullptr;
                    else p->next=cur.front();
                }
            }
        }
    };

     6.balanced-binary-tree

    class Solution {
    public:
        bool isBalanced(TreeNode *root) {
            if(!root)return 1;
            if(abs(maxDepth(root->left)-maxDepth(root->right))>1)return 0;
            if((!isBalanced(root->left))||(!isBalanced(root->right)))return 0;
            return 1;
        }
        int maxDepth(TreeNode *root){
            if(!root)return 0;
            return max(maxDepth(root->left),maxDepth(root->right))+1;
        }
    };

    7. path-sum

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

    8. payh-sum-ii

    解答:深度优先搜索

    class Solution {
    public:
        vector<vector<int> > pathSum(TreeNode *root, int sum) {
            vector<vector<int> > res;
            if(!root)return res;      
            vector<int> path;
            dfs(root, sum, res, path);
            return res;
        }
        void dfs(TreeNode *root, int sum, vector<vector<int> > &res, vector<int> &path){
            if(!root)return;
            path.push_back(root->val);
            if(root->val==sum&&!root->left&&!root->right)res.push_back(path);
            dfs(root->left, sum-root->val, res, path);
            dfs(root->right, sum-root->val, res, path);
            path.pop_back();
        }
    };

     9. recover-binary-search-tree

    思路:涉及二叉树就要想到它前序遍历是递增的。

    class Solution {
    public:
        TreeNode *pre, *a, *b;
        void swap(int &a, int &b){
            int temp = a;
            a = b;
            b = temp;
        }
        void recoverTreeCore(TreeNode *root) {
            if(root->left)recoverTreeCore(root->left);
            if(pre&&pre->val>root->val){
                if(!a)a = pre;
                b = root;
            }
            pre = root;
            if(root->right)recoverTreeCore(root->right);
        }
        void recoverTree(TreeNode *root) {
            pre = nullptr;
            a = nullptr;
            b = nullptr;
            if(!root)return;
            recoverTreeCore(root);
            if(a&&b)swap(a->val, b->val);
        }
    };
    

      

  • 相关阅读:
    19-background
    18-超链接导航栏案例
    17-文本属性和字体属性
    16-margin的用法
    15-浮动
    14-块级元素和行内元素
    13-标准文档流
    12-简单认识下margin
    11-border(边框)
    10-padding(内边距)
  • 原文地址:https://www.cnblogs.com/xctcherry/p/9010900.html
Copyright © 2020-2023  润新知