Leetcode 树

 

二叉树

1. Leetcode 100 Same Tree

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) 
    {
        if(p==NULL && q==NULL)  return true;
        if(p!=NULL && q!=NULL && p->val==q->val)
        {
            return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
        
        return false;
    }
};

 

2. Leetcode 101 Symmetric Tree

class Solution {
public:
    bool checkSymmetric(TreeNode* p, TreeNode* q)
    {
        if(p==NULL && q==NULL) return true;
        if(p==NULL || q==NULL) return false;
        if(p->val!=q->val) return false;
        else
            return checkSymmetric(p->left, q->right) && checkSymmetric(p->right, q->left);
    }
    
    bool isSymmetric(TreeNode* root) 
    {
        if(root==NULL) return true;
        
        return checkSymmetric(root->left, root->right);
    }
};

 

 3. Leetcode 111. Minimum Depth of Binary Tree

class Solution {
public:
    int minDepth(TreeNode* root) 
    {
        if(root==NULL)  return 0;
        
        if(root->left==NULL)
            return 1 + minDepth(root->right);
        else if(root->right==NULL)
            return 1 + minDepth(root->left);
        //if(root->left!=NULL && root->right!=NULL)
        return 1 + min(minDepth(root->left), minDepth(root->right));
    }
};

 

 

4. Leetcode 112. Path Sum

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

 

 

5. Leetcode 144. Binary Tree Preorder Traversal

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

 

 

6. Leetcode 94. Binary Tree Inorder Traversal

原理:

1) Create an empty stack S.
2) Initialize current node as root
3) Push the current node to S and set current = current->left until current is NULL
4) If current is NULL and stack is not empty then 
     a) Pop the top item from stack.
     b) Print the popped item, set current = popped_item->right 
     c) Go to step 3.
5) If current is NULL and stack is empty then we are done.
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) 
    {
        if(root==NULL)  return {};
        
        vector<int> res;
        stack<TreeNode*> s;
        
        while(true)
        {
            if(root!=NULL)
            {
                s.push(root);
                root = root->left;
            }
            
            else
            {
                if(s.empty())   break;
                
                root = s.top();
                s.pop();
                res.push_back(root->val);
                root = root->right;
            }
        }
        
        return res;
    }
};

 

 

7. Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return constructTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
}

TreeNode *constructTree(vector<int> &preorder, int pLeft, int pRight, vector<int> &inorder, int iLeft, int iRight) {
if (pLeft > pRight || iLeft > iRight) return NULL;
int i = 0;
for (i = iLeft; i <= iRight; ++i) {
if (preorder[pLeft] == inorder[i]) break;
}
TreeNode *cur = new TreeNode(preorder[pLeft]);
cur->left = constructTree(preorder, pLeft + 1, pLeft + i - iLeft, inorder, iLeft, i - 1);
cur->right = constructTree(preorder, pLeft + i - iLeft + 1, pRight, inorder, i + 1, iRight);
return cur;
}
};

 

 

8. 

 

二叉搜索树  

1. Leetcode 235 Lowest Common Ancestor of a Binary Search Tree

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        if(root->val> max(p->val, q->val))
            return lowestCommonAncestor(root->left, p, q);
        else if(root->val< min(p->val,q->val))
            return lowestCommonAncestor(root->right, p, q);
        
        else
            return root;
    }
};

 

2. Leetcode 230. Kth Smallest Element in a BST

class Solution {
public:
    int countNodes(TreeNode* root)
    {
        if(root==NULL)
            return 0;
        
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
    
    int kthSmallest(TreeNode* root, int k) 
    {
        if(root==NULL)
            return 0;
        
        int leftNodes = countNodes(root->left);
        
        if(leftNodes+1 == k)
            return root->val;
        else if(leftNodes >= k)
             return kthSmallest(root->left, k);
        else
            return kthSmallest(root->right, k-leftNodes-1);
        
    }
};

 

3.  Leetcode 108. Convert Sorted Array to Binary Search Tree

class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) 
    {
        return constructTree(nums, 0, nums.size()-1);
    }
    
    TreeNode* constructTree(vector<int>& nums, int left, int right)
    {
        if(left>right) return NULL;
        
        int mid = left + (right-left)/2;
        TreeNode* root = new TreeNode(nums[mid]);
        root->left = constructTree(nums, left, mid-1);
        root->right = constructTree(nums, mid+1, right);
        
        return root;
    }
};

 

 

4. 

 

posted @ 2020-04-16 22:47  jenny1000000  阅读(144)  评论(0编辑  收藏  举报