LeetCode-Explore-"Binary Tree"

本文主要探讨了LeetCode中与二叉树相关的四个经典问题:前序、中序、后序遍历,构建带下右指针的二叉树,查找二叉树的最近公共祖先,以及二叉树的序列化与反序列化。通过这些题目,深入理解二叉树的操作和特性。

1.Preorder,Inorder,Postorder

class Solution {
//recursive solution
//preorder
private:
    void preOrder(TreeNode* cur,vector<int> &res){
        if(cur==NULL) return;
        
        res.push_back(cur->val);
        preOrder(cur->left,res);
        preOrder(cur->right,res);
    }
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        preOrder(root,res);
        return res;
    }
};
/** Iterative solution for inorder
 * 先走到最左边,然后对点进行操作,再去右边
 */
vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> search;
        vector<int> res;
        TreeNode* cur=root;
        
        while(cur!=NULL||!search.empty()){
            while(cur){
                search.push(cur);
                cur=cur->left;
            }
            
            cur=search.top();
            search.pop();
            res.push_back(cur->val);
            cur=cur->right;
            
        }
       return res;
    }

2.Populationg next right pointers in each node II

class Solution {
public:
    void connect(TreeLinkNode *root) {
        TreeLinkNode* cur=root;
        TreeLinkNode* head=NULL;//head of next level
        TreeLinkNode* prev=NULL;//previous node
        
        while(cur!=NULL){
            while(cur!=NULL){
                if(cur->left){
                    if(prev){
                        prev->next=cur->left;
                    }else{
                        head=cur->left;
                    }
                    prev=cur->left;
                }
                if(cur->right){
                    if(prev){
                        prev->next=cur->right;
                    }else{
                        head=cur->right;
                    }
                    prev=cur->right;
                }
                cur=cur->next;
            }
            cur=head;
            head=NULL;
            prev=NULL;
        }
    }
};

3.lowest common ancestor of binary tree

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root==NULL||root==p||root==q)
            return root;
        
        TreeNode* parent1=lowestCommonAncestor(root->left,p,q);
        TreeNode* parent2=lowestCommonAncestor(root->right,p,q);
        
        if(parent1&&parent2)
            return root;
        else
            return parent1? parent1:parent2;
    }
};

4.serialize and deserialize binary tree

class Codec {
/**总结下 istringstream和ostringstream
 * ostringstream 用来构造string,操作符 << 表示向其中写入对象
 * istringstream 用来从string中读取对象,以空格为间断
 */
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        ostringstream out;
        serial(root,out);
        return out.str();
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        istringstream in(data);
        return deserial(in);
    }
private:
    void serial(TreeNode* cur,ostringstream &out){
        if(cur){
            out<<cur->val<<' ';
            serial(cur->left,out);
            serial(cur->right,out);
        }else{
            out<<"# ";
        }
        
    }
    
    TreeNode* deserial(istringstream &in){
        string cur;
        in>>cur;
        if(cur=="#")
            return nullptr;
        else{
            TreeNode* temp=new TreeNode(stoi(cur));
            temp->left=deserial(in);
            temp->right=deserial(in);
            return temp;
        }
        
    }
    
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值