简体   繁体   中英

How do I invert a binary search tree?

Problem : https://leetcode.com/problems/invert-binary-tree/

My approach :

void getInorder(std::vector<int> & vec, TreeNode* root){
        if(root){
            getInorder(vec, root->left);
            vec.push_back(root->val);
            getInorder(vec, root->right);
        }
    }

    void setValues(TreeNode*  root, std::vector<int> &vec, int &i ){
        if(root){
            setValues(root->left, vec, i);
            root->val = vec[i];
            i--;
            setValues(root->right, vec, i);
        }
    }

    TreeNode* invertTree(TreeNode* root) {
        std::vector<int> inorderList, sol;
        getInorder(inorderList, root);
        int  i = inorderList.size() - 1;
        setValues(root, inorderList, i);
        getInorder(sol, root);
        return root;
    }

I am traversing the tree in inorder manner and I note down the values in a list. I again traverse the tree in inorder fashion but this time I start assigning values from the end of the list to the nodes. This should swap the left and right child values thus providing a decreasing sequence if you do inorder again.

The test case [1,2] fails. According to the logic it outputs [2,1] which seems right to me but the actual output is [1, null, 2]. I guess leetcode is doing a preorder traversal for the output, it is not very clear.

How do I fix this?

The code only changes the val fields, without changing the original topology. So a tree that looks like:

  1
 / \
2   5
   / \
  3   4

Can change only the values, so that in-order traversal will generate a reversed list:

  5
 / \
4   3
   / \
  1   2

This looks much better with binary search trees, but that is not the original problem description. With BST this reversal produces a BST with a reversed comparison operator.

Fortunately, usually tree reversal means a complete reversal of the tree, including its topology. It is fortunately, since getting a complete reversal is simpler to code than just value reversal. In the above example, a complete reversal of the original produces:

    1
   / \
  5   2
 / \
4   3

To get that you need to stop using a vector and stop collecting values. The algorithm becomes a trivial one-function recursive algorithm. It can be done with a function with a body of 6 lines.

If however you want to create a reversed copy, and not mutate the original tree, then it becomes a 7 line body.

Recursive Approach:

    public TreeNode invertTree(TreeNode root) {
        return invert(root);
    }

    public TreeNode invert(TreeNode root) {
        //base case 1
        if (root == null)
            return null;
        //base case 2
        if ((root.left == null) && (root.right == null)) {
            return root;
        } else {

            TreeNode leftt = invert(root.left);
            TreeNode rightt = invert(root.right);
            //swap nodes
            root.right = leftt;
            root.left = rightt;
            return root;
        }
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM