[Leetcode] 99, 114, 117

99. Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O( n ) space is pretty straight forward. Could you devise a constant space solution?


Solution: 对BST进行中序遍历后,会得到一个有序的递增数组。其中有两个数交换的位置,因此此题会蜕化为一个有序数组中两个数据互换位置,怎样找到这两个数的问题。

原数组:{1, 2, 3, 4, 5, 6},x=5,y=2,交换x和y可得,

交换后:{1,5, 3, 4, 2, 6}

可以看出,在交换之后,x一定比后一位要大,而且在它之前都不会有满足这个条件的数,因此可以通过这个判断条件找到x的位置。而y则是x之后比前一位要小的数。但是要注意的是,x的后一位(即3)也是满足这个条件的,但是它有可能是y也可能不是y,这时需要将y初始化为这一位,然后往后搜索,如果能找到第二个满足这个条件的数就进行替换,否则y就是初始值。

Code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void recoverTree(TreeNode* root) {
        vector<TreeNode*> v;
        inorderTraversal(root, v);
        int x=-1,y=-1;
        int i;
        for(i=0; i<v.size(); i++)
            if(v[i]->val>v[i+1]->val){
                x = i;
                break;
            }
        if(x<0) return;
        y = x+1;
        for(i=i+2; i<v.size(); i++)
            if(v[i]->val<v[i-1]->val){
                y = i;
                break;
            }
        swap(v[x]->val, v[y]->val);
    }
private:
    void inorderTraversal(TreeNode* root, vector<TreeNode*>& v){
        if(root==NULL) return;
        inorderTraversal(root->left, v);
        v.push_back(root);
        inorderTraversal(root->right, v);
    }
    
};



114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:
   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
             6

Solution(1): 迭代,Morris中序遍历的变体。

Code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void flatten(TreeNode* root) {
        TreeNode* cur = root;
        while(cur!=NULL){
            if(cur->left==NULL){
                cur = cur->right;
            }else{
                if(cur->right!=NULL){
                    TreeNode* node = cur->left;
                    while(node->right!=NULL) node = node->right;
                    node->right = cur->right;
                }
                cur->right = cur->left;
                cur->left = NULL;
                cur = cur->right;
            }
        }
    }
};

Solution(2):递归。

Code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void flatten(TreeNode* root) {
        if(root==NULL) return;
        
        flatten(root->left);
        flatten(root->right);
        
        TreeNode* tail = root->left;
        if(tail==NULL) return;
        
        while(tail->right!=NULL) tail = tail->right;
        tail->right = root->right;
        root->right = root->left;
        root->left = NULL;
    }
};



117. Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

Solution: 树的level-order遍历的变体,使用queue记录,进行遍历即可。

Code:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(root==NULL) return;
        queue<TreeLinkNode*> cur;
        queue<TreeLinkNode*> next;
        cur.push(root);
        TreeLinkNode* p = NULL;
        while(!cur.empty()){
            while(!cur.empty()){
                p = cur.front(); cur.pop();
                if(p->left!=NULL) next.push(p->left);
                if(p->right!=NULL) next.push(p->right);
                if(!cur.empty()) p->next = cur.front();
                else p->next = NULL;
            }
            swap(cur,next);
        }
    }
};

相关题目: 116. Populating Next Right Pointers in Each Node

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值