GVKun编程网logo

[LeetCode] Flatten Binary Tree to Linked List

18

对于[LeetCode]FlattenBinaryTreetoLinkedList感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于#Leetcode#114.FlattenBinaryTreet

对于[LeetCode] Flatten Binary Tree to Linked List感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于#Leetcode# 114. Flatten Binary Tree to Linked List、#Leetcode# 951. Flip Equivalent Binary Trees、114. Flatten Binary Tree to Linked List、114. Flatten Binary Tree to Linked List - Medium的有用信息。

本文目录一览:

[LeetCode] Flatten Binary Tree to Linked List

[LeetCode] Flatten Binary Tree to Linked List

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

For example,given the following tree:

    1
   /   2   5
 / \   3   4   6

The flattened tree should look like:

1
   2
       3
           4
               5
                   6

将一个二叉树重组成一个链表

题目要求按照二叉树的先序遍历的顺序重组二叉树

思路1:

找到最左侧结点,将其父结点与父结点右结点断开,将其连接至父结点右侧,变成父结点的右结点,然后把原右结点插入到新右结点的右侧。递归这一操作即可

/**
 * 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)
            return;
        if (root->left)
            flatten(root->left);
        if (root->right)
            flatten(root->right);
        TreeNode* temp = root->right;
        root->right = root->left;
        root->left = NULL;
        while (root->right)
            root = root->right;
        root->right = temp;
    }
};

思路2:

从根结点出发,判断其左结点是否存在,如果存在,则将根结点与其右结点断开,根的左结点变成其右结点。在将右结点链接至左结点最右边的右结点处。

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

#Leetcode# 114. Flatten Binary Tree to Linked List

#Leetcode# 114. Flatten Binary Tree to Linked List

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/

 

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

For example,given the following tree:

    1
   /   2   5
 / \   3   4   6

The flattened tree should look like:

1
   2
       3
           4
               5
                   6

代码:

/**
 * 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) return;
        flatten(root -> left);
        flatten(root -> right);
        
        TreeNode* R = root -> right;
        root -> right = root -> left;
        root -> left = NULL;
        TreeNode* cur = root;
        while(cur -> right) cur = cur -> right;
        cur -> right = R;
    }
};

  好像一个世纪那么久没写 LC 了  

#Leetcode# 951. Flip Equivalent Binary Trees

#Leetcode# 951. Flip Equivalent Binary Trees

https://leetcode.com/problems/flip-equivalent-binary-trees/

 

For a binary tree T,we can define a flip operation as follows: choose any node,and swap the left and right child subtrees.

A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.

Write a function that determines whether two binary trees are flip equivalent.  The trees are given by root nodes root1 and root2.

 

Example 1:

Input: root1 = [1,2,3,4,5,6,null,7,8],root2 = [1,8,7] Output: true Explanation: We flipped at nodes with values 1,and 5. 

Flipped Trees Diagram

 

Note:

  1. Each tree will have at most 100 nodes.
  2. Each value in each tree will be a unique integer in the range [0,99].

代码:

/**
 * 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:
    bool flipEquiv(TreeNode* root1,TreeNode* root2) {
        if(!root1 && !root2) return true;
        if(!root1 || !root2) return false;
        if(root1 -> val != root2 -> val) return false;
        return flipEquiv(root1 -> left,root2 -> right) && flipEquiv(root1 -> right,root2 -> left) ||
            flipEquiv(root1 -> left,root2 -> left) && flipEquiv(root1 -> right,root2 -> right);
    }
};

  为什么要 

分享图片

 这个判断

114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

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

For example, given the following tree:

    1
   / \
  2   5
 / \   \
3   4   6

The flattened tree should look like:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

难度:medium

题目:给定二叉树,原地扁平化。

思路:后序遍历

Runtime: 6 ms, faster than 100.00% of Java online submissions for Flatten Binary Tree to Linked List.
Memory Usage: 40 MB, less than 100.00% of Java online submissions for Flatten Binary Tree to Linked List.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void flatten(TreeNode root) {
        rightFlatten(root);
    }
    
    private TreeNode rightFlatten(TreeNode root) {
        if (null == root) {
            return root;
        }
        TreeNode left = rightFlatten(root.left);
        TreeNode right = rightFlatten(root.right);
        if (left != null) {
            root.left = null;
            root.right = left;
            TreeNode rightMost = left;
            while (rightMost.right != null) {
                rightMost = rightMost.right;
            }
            rightMost.right = right;
        }
        
        return root;
    }
}

114. Flatten Binary Tree to Linked List - Medium

114. Flatten Binary Tree to Linked List - Medium

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

For example,given the following tree:

    1
   /   2   5
 / \   3   4   6

The flattened tree should look like:

1
   2
       3
           4
               5
                   6

 

每个节点的右节点都是preorder traverse的下一个节点 -> 应该先 反preorder遍历 (即node.right->node.left->node) 到最后,从最后开始relink

time: O(n),space: O(height)

@H_301_22@/**@H_301_22@
 * DeFinition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 @H_301_22@*/
class Solution {
    TreeNode prev = null;
    
    public void flatten(TreeNode root) {
        if(root == null) {
            return;
        }
        
        flatten(root.right);
        flatten(root.left);
        
        root.right = prev;
        root.left = null;
        prev = root;
    }
}

我们今天的关于[LeetCode] Flatten Binary Tree to Linked List的分享就到这里,谢谢您的阅读,如果想了解更多关于#Leetcode# 114. Flatten Binary Tree to Linked List、#Leetcode# 951. Flip Equivalent Binary Trees、114. Flatten Binary Tree to Linked List、114. Flatten Binary Tree to Linked List - Medium的相关信息,可以在本站进行搜索。

本文标签: