【刷题笔记20 二叉树 part06】

二叉树操作

最大二叉树

654.最大二叉树

法1:

	TreeNode* traversal(vector<int>& nums, int left, int right){
        if (left >= right) return nullptr;
         //分割点下标
         int max_value_index = left;
        for (int i = left + 1; i < right; ++i) {
            if (nums[i] > nums[max_value_index]) max_value_index = i;
        }
        
        TreeNode* root = new TreeNode(nums[max_value_index]);
        //左子树 左闭右开
        root->left = traversal(nums,left,max_value_index);
        root->right = traversal(nums,max_value_index+1,right);
        
        return root;
    }
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
       return traversal(nums, 0, nums.size());
    }

法2:

TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        int len = nums.size();
        TreeNode* node = new TreeNode(0);
        if (len == 1){
            node->val = nums[0];
            return node;
        }
        
        //找到数组中的最大值
        int max_value = 0;
        int max_value_index = 0;
        for (int i = 0; i < len; ++i) {
            if (nums[i] > max_value){
                max_value = nums[i];
                max_value_index = i;
            } 
        }
        node->val = max_value;
        
        //子区间 最大值左
        if (max_value_index > 0){
            vector<int> new_vector(nums.begin(),nums.begin() + max_value_index);
            node->left = constructMaximumBinaryTree(new_vector);
        }

        //子区间 最大值右
        if (max_value_index < len - 1){
            vector<int> new_vector(nums.begin() + max_value_index + 1,nums.end());
            node->right = constructMaximumBinaryTree(new_vector);
        }
        return node;
    }

合并二叉树

617.合并二叉树
法1: 递归

   
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        //递归
        if (root1 == NULL) return root2;
        if (root2 == NULL) return root1;
        
        //前序遍历 修改root1
        //root1->val += root2->val;
        
        root1->left = mergeTrees(root1->left,root2->left);
        
        //root1->val += root2->val;
        root1->right = mergeTrees(root1->right,root2->right);
        //后序遍历
        root1->val += root2->val;
        return root1;*/
    }

法2:迭代

TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
       
       //迭代
        if (root1 == NULL) return root2;
        if (root2 == NULL) return root1;
        queue<TreeNode*> que;
        
        que.push(root1);
        que.push(root2);

        while (!que.empty()){
            TreeNode* node1 = que.front();que.pop();
            TreeNode* node2 = que.front();que.pop();
            
            node1->val += node2->val;

            if (node1->left != NULL && node2->left != NULL){
                que.push(node1->left);
                que.push(node2->left);
            }

            if (node1->right != NULL && node2->right != NULL){
                que.push(node1->right);
                que.push(node2->right);
            }

            if (node1->left == NULL && node2->left != NULL){
                node1->left = node2->left;
            }

            if (node1->right == NULL && node2->right != NULL){
                node1->right = node2->right;
            }
        }
        return root1;
    }

二叉搜索树中的搜索

700.二叉搜索树中的搜索

法1:递归

    	TreeNode* searchBST(TreeNode* root, int val) {
    	 if (root == NULL || val == root->val) return root;
        //TreeNode* ans = NULL;

        if (root->val > val) return searchBST(root->left, val);
        if (root->val < val) return searchBST(root->right, val);
        
        return NULL;
        }

法2: 迭代

TreeNode* searchBST(TreeNode* root, int val) {
 //迭代
        while(root != NULL){
            if (root->val > val) root = root->left;
            else if (root->val < val) root = root->right;
            else return root;
        }
        return NULL;
}

验证二叉搜索树

98.验证二叉搜索树

法1:递归

	 bool isValidBST(TreeNode* root) {
//        //递归
//        if (!root)return true;
//        bool left = isValidBST(root->left);
//        if (pre != NULL  && root->val <= pre->val)return false;
//        pre = root;
//
//        bool right = isValidBST(root->right);
//        return left && right;
		}

法2:迭代


     bool isValidBST(TreeNode* root) {
          //迭代
          stack<TreeNode*> st;
          TreeNode* pre = NULL;
          TreeNode* cur = root;
          while(cur != NULL || !st.empty()){
              if (cur != NULL){
                  st.push(cur);
                  cur = cur->left;
              } else{
                  cur = st.top();
                  st.pop();
                  if (pre != NULL && cur->val <= pre->val)return false;
                  pre = cur;
                  cur = cur->right;
              }
          }
        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值