二叉树遍历


/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型vector
     */
     // 前序
      vector<int> preorderTraversal(TreeNode* root) {
         // write code here
        vector<int> res;
        if (root == nullptr) return res;
        res.push_back(root->val);

    
        auto lList = preorderTraversal(root->left);
        res.insert(res.end(), lList.begin(), lList.end());

        // 右树递归返回列表
        auto rList = preorderTraversal(root->right);
        res.insert(res.end(), rList.begin(), rList.end());
        
        return res;
    }
    // 中序
vector<int> inorderTraversal(TreeNode* root) {
        // write code here
        vector<int> res;
        if (root == nullptr) return res;
        
        auto leftList = inorderTraversal(root->left);
        res.insert(res.end(), leftList.begin(), leftList.end());
        res.push_back(root->val);
        
        auto rightList = inorderTraversal(root->right);
        res.insert(res.end(), rightList.begin(), rightList.end());
        
        return res;
    }

// 后序
vector<int> postorderTraversal(TreeNode* root) {
        // write code here
        vector<int> res;
        if (root == nullptr) return res;
        auto lList = postorderTraversal(root->left);
        res.insert(res.end(), lList.begin(), lList.end());
        
        
        auto rList = postorderTraversal(root->right);
        res.insert(res.end(), rList.begin(), rList.end());
        
        res.push_back(root->val);
        
        return res;
    }


	// 层遍历
	/**
     * 
     * @param root TreeNode类 
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > levelOrder(TreeNode* root) {
        // write code here
        vector<vector<TreeNode*>> treeNodeList{{root}};
        
        vector<vector<int>> res;
        int index = 0;
        while(index < treeNodeList.size()) {
            auto elem = treeNodeList[index++];
            if (elem.size() == 0) continue;
            vector<int> inRes;
            for (const auto& it: elem) {
                if (it == nullptr) continue;
                inRes.push_back(it->val);
                vector<TreeNode*> lrNodeVec{it->left, it->right};
                treeNodeList.push_back(lrNodeVec);
            }
            if (inRes.size()!=0) res.push_back(inRes);
        }
        return res;
    }
};

// 获取二叉树的最大深度
int maxDepth(TreeNode* root) {
        // write code here
        if (root == nullptr) return 0;
        std::multimap<int, TreeNode*> rootMap;
        rootMap.insert(std::pair<int, TreeNode*>(1, root));
        vector<multimap<int, TreeNode*>> nodeList;
        nodeList.push_back(rootMap);
        int index = 0;
        int depth = 0;
        while(index < nodeList.size()){
            auto elem = nodeList[index++];
            
            auto subMap = std::multimap<int, TreeNode*>();
            for (const auto& it: elem) {
                
                depth = it.first>depth?it.first:depth;
                if (it.second->left != nullptr) subMap.insert(std::pair<int, TreeNode*>(it.first+1, it.second->left));
                if (it.second->right != nullptr) subMap.insert(std::pair<int, TreeNode*>(it.first+1, it.second->right));
            }
            if (subMap.size() != 0) nodeList.push_back(subMap);
        }
        return depth;
        
    }
	// 校验二叉树是否对称
	bool isSymmetrical(TreeNode* pRoot) {
//         if (pRoot == nullptr) return true;
//         vector<vector<TreeNode*>> treeNodeList{{pRoot}};
//         int idx  = 0;
//         while(idx < treeNodeList.size()) {
//             auto depthList = treeNodeList[idx++];    
//             vector<TreeNode*> tmpList;
//             bool isAllNull = true;
//             for (const auto& it: depthList) {
//                 if (it != nullptr) isAllNull = false;
//                 tmpList.push_back(it == nullptr?nullptr:it->left);
//                 tmpList.push_back(it==nullptr?nullptr:it->right);
//             }
//             if (isAllNull) return true;
//             auto tmpLen = tmpList.size();
//             for (int sidx = 0, eidx = tmpLen-1 ; sidx< tmpLen/2 && eidx>=tmpLen/2; sidx++, eidx--) {
//                 if (tmpList[sidx] == nullptr && tmpList[eidx] == nullptr) continue;
//                 if ((tmpList[sidx] == nullptr && tmpList[eidx] != nullptr) || (tmpList[sidx] != nullptr && tmpList[eidx] == nullptr)) return false;
//                 if (tmpList[sidx]->val != tmpList[eidx]->val) return false;
//             }
//             treeNodeList.push_back(tmpList);
//         }
//         return true;
        // 根左右和根右左遍历结果应该完全相同
        // 递归终止条件,节点左右都为空
        if (pRoot == nullptr) return true;
        
        return isSymmetrical(pRoot->left, pRoot->right); 
    }
    bool isSymmetrical(TreeNode*pLeft, TreeNode*pRight) {
        if (pLeft == nullptr && pRight== nullptr) return true;
        if ((pLeft && !pRight) || (!pLeft && pRight)) return false;
        return pLeft->val == pRight->val && isSymmetrical(pLeft->left, pRight->right) &&
            isSymmetrical(pLeft->right, pRight->left);
    }

	// 合并二叉树
	TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        // write code here
        TreeNode* res = nullptr;
        if (t1 == nullptr && t2 == nullptr ) return res;
        if (!t1 && t2) {
           res = t2;
		}else if (t1 && !t2) {
            res = t1;
        }else {
            res = t1;
            res->val = t1->val+t2->val;
            res->left = mergeTrees(t1->left, t2->left);
            res->right = mergeTrees(t1->right, t2->right);
        }
        return res;
    }
// 镜像
TreeNode* Mirror(TreeNode* pRoot) {
        // write code here
//         if (pRoot == nullptr) return nullptr;
//         TreeNode* res = (TreeNode*)malloc(sizeof(TreeNode));
//         res->val = pRoot->val;
//         res->left = Mirror(pRoot->right);
//         res->right = Mirror(pRoot->left);
//         return res;
            
            if (pRoot == nullptr) return pRoot;
            auto tmp = pRoot->left;
            pRoot->left = pRoot->right;
            pRoot->right = tmp;
            
            Mirror(pRoot->left);
            Mirror(pRoot->right);
            return pRoot;
            
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值