Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

114. Flatten Binary Tree to Linked List #281

Open
Tcdian opened this issue Aug 2, 2020 · 1 comment
Open

114. Flatten Binary Tree to Linked List #281

Tcdian opened this issue Aug 2, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Aug 2, 2020

114. Flatten Binary Tree to Linked List

给定一个二叉树,原地将它展开为链表。

例如,给定二叉树

    1
   / \
  2   5
 / \   \
3   4   6

将其展开为:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6
@Tcdian
Copy link
Owner Author

Tcdian commented Aug 2, 2020

Solution 1

  • JavaScript Solution
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {void} Do not return anything, modify root in-place instead.
 */
var flatten = function(root) {
    if (root === null) {
        return;
    }
    flatten(root.left);
    flatten(root.right);
    const next = root.right;
    root.right = root.left;
    root.left = null;
    while (root.right !== null) {
        root = root.right;
    }
    root.right = next;
};
  • TypeScript Solution
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

/**
 Do not return anything, modify root in-place instead.
 */
function flatten(root: TreeNode | null): void {
    if (root === null) {
        return;
    }
    flatten(root.left);
    flatten(root.right);
    const next = root.right;
    root.right = root.left;
    root.left = null;
    while (root.right !== null) {
        root = root.right;
    }
    root.right = next;
};

Solution 2

  • JavaScript Solution
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {void} Do not return anything, modify root in-place instead.
 */
var flatten = function(root) {
    let current = root;
    while(current !== null) {
        if (current.left !== null) {
            const next = current.left;
            let patrol = next;
            while(patrol.right !== null) {
                patrol = patrol.right;
            }
            patrol.right = current.right;
            current.right = next;
            current.left = null;
        }
        current = current.right;
    }
};
  • TypeScript Solution
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

/**
 Do not return anything, modify root in-place instead.
 */
function flatten(root: TreeNode | null): void {
    if (root === null) {
        return;
    }
    flatten(root.left);
    flatten(root.right);
    let patrol = root.left;
    if (patrol !== null) {
        const next = root.right;
        root.right = patrol;
        while(patrol.right !== null) {
            patrol = patrol.right;
        }
        patrol.right = next;
        root.left = null;
    }
};

@Tcdian Tcdian added the Tree label Aug 8, 2020
@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant