当前位置:编程学习 > C/C++ >>

leetcode代码分类汇总之-树

leetcode中关于树的题目汇总,这部分题目比较多:

 


Balanced Binary Tree

 

[cpp]  class Solution { 
public: 
    int subBal(TreeNode* root){ 
        if(root==NULL) 
            return 0; 
        int left = subBal(root->left); 
        int right = subBal(root->right); 
        if(left<0||right<0||abs(left-right)>1) return -1; 
        return max(left,right)+1; 
    } 
    bool isBalanced(TreeNode *root) { 
        return subBal(root)>=0; 
    } 
}; 

class Solution {
public:
    int subBal(TreeNode* root){
        if(root==NULL)
            return 0;
        int left = subBal(root->left);
        int right = subBal(root->right);
        if(left<0||right<0||abs(left-right)>1) return -1;
        return max(left,right)+1;
    }
    bool isBalanced(TreeNode *root) {
        return subBal(root)>=0;
    }
};
Binary Tree Inorder Traversal


这个给出两种实现,第一种是网上的,第二种是用我说的比较通用的转化方法做的:


[cpp]  class Solution { 
public: 
    vector<int> inorderTraversal(TreeNode *root) { 
        vector<int> ans; 
        if(root==NULL) return ans; 
        stack<TreeNode*> s; 
        while(root!=NULL || !s.empty()) 
        { 
            while(root!=NULL) 
            { 
                s.push(root); 
                root = root->left; 
            } 
            if(!s.empty()) 
            { 
                root = s.top(); 
                s.pop(); 
                ans.push_back(root->val); 
                root = root->right; 
            } 
        } 
        return ans; 
    } 
}; 

class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> ans;
        if(root==NULL) return ans;
        stack<TreeNode*> s;
        while(root!=NULL || !s.empty())
        {
            while(root!=NULL)
            {
                s.push(root);
                root = root->left;
            }
            if(!s.empty())
            {
                root = s.top();
                s.pop();
                ans.push_back(root->val);
                root = root->right;
            }
        }
        return ans;
    }
};
[cpp] class Solution { 
public: 
    struct Node{ 
        TreeNode* node_; 
        bool sign; 
        Node(TreeNode* n){node_=n;sign=false;} 
    }; 
    vector<int> inorderTraversal(TreeNode *root) { 
        stack<Node> stk; 
        stk.push(Node(root)); 
        vector<int> result; 
        if(!root) return result; 
        while(!stk.empty()){ 
            Node tmp = stk.top(); 
            stk.top().sign = true; 
            if(tmp.sign){ 
                result.push_back(tmp.node_->val); 
                stk.pop(); 
                if(tmp.node_->right) 
                  stk.push(Node(tmp.node_->right)); 
            }else if(tmp.node_->left) 
              stk.push(Node(tmp.node_->left)); 
        } 
        return result; 
    } 
}; 

class Solution {
public:
    struct Node{
        TreeNode* node_;
        bool sign;
        Node(Tree

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,