LeetCode114 Expand binary tree into linked list---three solutions to binary tree problem (recursion) (iteration) (predecessor node)

Expand binary tree to linked list

LeetCode 114: Expand a binary tree into a linked list
Description:
Given the root node root of a binary tree, please expand it into a singly linked list:

  • The expanded singly linked list should also use TreeNode, where the right child pointer points to the next node in the linked list, and the left child pointer is always null.
  • The expanded singly linked list should be in the same preorder traversal order as the binary tree.
    insert image description here

Solution 1: Recursive solution

Problem solving ideas:

  1. Using the method of preorder traversal, add to listit
  2. Traversing listthe elements, let ithe left subtree of the subscript node be empty and the right subtree point i+1to the subscript node.

Code:

class Solution {
    
    
    public void flatten(TreeNode root) {
    
    
        List<TreeNode> list = new ArrayList<TreeNode>();
        preorderTraversal(root, list);
        for (int i = 0; i < list.size() - 1; i++) {
    
    
            list.get(i).left = null;
            list.get(i).right = list.get(i+1);
        }
    }
    public void preorderTraversal(TreeNode root, List<TreeNode> list) {
    
    
        if (root == null) return;
        list.add(root);
        preorderTraversal(root.left, list);
        preorderTraversal(root.right, list);
    }
}

Solution 2: Iterative solution

Problem solving ideas:

  1. Use the last-in-first-out feature of the stack to record the previous node.
  2. If the stack is not empty, or the node is not empty, enter the loop, if the node is not empty, push the stack, and insert it into listit. Loop the left subtree of the node until it is empty to stop the loop.
  3. If the left subtree is empty, pop the element at the top of the stack and loop through the right subtree of the node at the top of the stack.
  4. If the stack is empty and the nodes have been traversed, the loop ends.
  5. Then traverse the list and the steps of method one are the same.

Code:

class Solution {
    
    
    public void flatten(TreeNode root) {
    
    
        if(root == null) return;
        Stack<TreeNode> stack = new Stack<>();
        List<TreeNode> list = new ArrayList<>();
        TreeNode cur = root;
        //cur 不为空 或 栈 不为空 进入循环
        while(cur != null || !stack.isEmpty()){
    
    
            while(cur != null){
    
    
                stack.push(cur);//cur不为空就入栈
                list.add(cur);//入栈的同时插入到list中
                cur = cur.left;//根->左的顺序
            }
            TreeNode top = stack.pop();
            cur = top.right;//根->左 结束之后 就开始 右
        }
        //遍历list
        for (int i = 0; i < list.size() - 1; i++) {
    
    
            list.get(i).left = null;
            list.get(i).right = list.get(i+1);
        }
    }
}

Solution 3: The space complexity of the precursor node is O(1)

Problem solving ideas:

  1. Always make the right subtree of the root node connect to the right subtree of the left subtree, make the left subtree of the root become the right subtree of the root, leave the left subtree empty, and let root = root.right.
  2. If the root node is not null, the reference curpoints to root.
  3. If rootthe left subtree is not empty, refer to a curNextpointer to root.left;
  4. Quote a prepointer curNext, loop until pre.right = null, if pre.right != null, let pre = pre.right. If the loop ends, pre.right == null. let pre.right = cur. right.
  5. let cur.left = null, cur.right = curNext; cur = cur.right;
  6. Repeat steps 2~4.

Drawing analysis:

insert image description here

Code:

class Solution {
    
    
    public void flatten(TreeNode root) {
    
    
        if(root == null) return;
        TreeNode cur = root;
        TreeNode curNext = null;
        while (cur != null){
    
    
            if(cur.left != null){
    
    
                curNext = cur.left;
            	TreeNode pre = curNext;
            	//让pre指向左子树的最右节点
           		while (pre.right != null){
    
    
                	pre = pre.right;
            	}
            	//让左子树最右节点和右子树链接
	           	pre.right = cur.right;
            	//让根节点的左边断开
            	cur.left = null;
            	//让根节点的右子树连接左子树
            	cur.right = curNext;
            }
            cur = cur.right;
        }
    }

}

Guess you like

Origin blog.csdn.net/wwzzzzzzzzzzzzz/article/details/122172287