LeetCode.897-Increasing Order Search Tree

This is the 346th update to Pleasure, 370th original

01 Questions and preparation

Today, we introduce topic 211 of the Easy level in the LeetCode algorithm (the title number in sequence is 897).Given a tree, rearrange the tree in a middle traversal order so that the leftmost node in the tree is now the root of the tree, and each node has no left child node and only one right child node.For example:
Input: [5,3,6,2,4, null, 8,1, null, null, null, 7,9]

       5
      / \
    3    6
   / \    \
  2   4    8
 /        / \ 
1        7   9

Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

 1
  \
   2
    \
     3
      \
       4
        \
         5
          \
           6
            \
             7
              \
               8
                \
                 9  

Be careful:

  • The number of nodes in a given tree will be between 1 and 100.

  • Each node has a unique integer value of 0 to 1000.

02 First Solution

The original binary tree is iterated through recursively, all node values are stored in a List, the first element in the List is used as the root node, the remaining elements in the List are iterated through as the right child node of the tree, and the new tree is returned.

public TreeNode increasingBST(TreeNode root) {
    List<Integer> list = new ArrayList<Integer>();
    inorder(root, list);
    TreeNode result = new TreeNode(list.get(0));
    TreeNode ans = result;
    for (int i=1; i<list.size(); i++) {
        ans.right = new TreeNode(list.get(i));
        ans = ans.right;
    }
    return result;
}

public void inorder(TreeNode node, List<Integer> list){
    if (node == null) {
        return ;
    }    
    inorder(node.left, list);
    list.add(node.val);
    inorder(node.right, list);
}


03 Second Solution

The idea is the same as the first solution, except that the middle-order traversal binary tree is replaced by iteration from recursion.

public TreeNode increasingBST2(TreeNode root) {
    List<Integer> list = new ArrayList<Integer>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    while (root != null || !stack.isEmpty()) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
        if (!stack.isEmpty()) {
            root = stack.pop();
            list.add(root.val);
            root = root.right;
        }
    }
    TreeNode result = new TreeNode(list.get(0));
    TreeNode ans = result;
    for (int i=1; i<list.size(); i++) {
        ans.right = new TreeNode(list.get(i));
        ans = ans.right;
    }
    return result;
}


04 Third Solution

We can also simplify that instead of using List to store the node values of the original binary tree, we can simply use the resulting node values as the node values of the new binary tree.

public TreeNode increasingBST3(TreeNode root) {
    TreeNode result = new TreeNode(0);
    TreeNode ans = result;
    Stack<TreeNode> stack = new Stack<TreeNode>();
    while (root != null || !stack.isEmpty()) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
        if (!stack.isEmpty()) {
            root = stack.pop();
            // Process nodes directly as the right child of a new tree
            ans.right = new TreeNode(root.val);
            ans = ans.right;
            root = root.right;
        }
    }
    return result.right;
}


05 Fourth Solution

For the third solution above, we can also use recursion.

TreeNode ans;
public TreeNode increasingBST4(TreeNode root) {
    TreeNode result = new TreeNode(0);
    ans = result;
    helper(root);
    return result.right;
}

public void helper(TreeNode root) {
    if (root == null) {
        return ;
    }
    helper(root.left);
    ans.right = new TreeNode(root.val);
    ans = ans.right;
    helper(root.right);
}


06 Summary

Algorithmic topics have been more than six months in a row. Algorithmic articles 214 + articles, public number dialog box reply to any keywords in [Data Structure and Algorithms], [Algorithms], [Data Structure], to get a collection of articles.

Above is all content, if you have any good solution ideas, suggestions or other issues, you can leave a message below to exchange, comment, forward is my greatest reward and support!

Posted by CK9 on Fri, 07 Jun 2019 11:10:57 -0700