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

Accepted

218,918

Submissions

533,947

【解析】由上图可知,如果右子树不为空,则右子树最后肯定为左子树最有一个靠右的孩子节点的右子树,而左子树最后成为整棵树的右子树。这样,首先判断左子树是否为空,不为空就寻找到树根的左孩子节点,然后寻找该节点是否有右孩子,如果有继续寻找,直到找到属于叶子节点的右孩子,此时,该节点的右子树“指向”当前树的右子树,并将当前左子树变为树根的右孩子,将整棵树左孩子置为空。最后,根节点“指向”根节点的右孩子,继续上述操作,直到整棵树遍历完即得到结果。

1.首先,将根节点的右子树,连接到根节点的左子树的最右节点,如下图所示:

2.然后将左子树移动到右边,如下图所示:

3.之后,将根节点的右节点 作为 当前的根节点,循环进行。

                              

/**
 * 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) {
        while(root != null) {
            if(root.left != null) {
                TreeNode pre = root.left;
                while(pre.right != null) {
                    pre = pre.right;
                }
                pre.right = root.right;
                root.right = root.left;
                root.left = null;
            }
            root = root.right;
        }
    }
}

原文地址:https://www.cnblogs.com/Roni-i/p/10434572.html

时间: 2024-10-04 07:43:58

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->nod

刷题114. Flatten Binary Tree to Linked List

一.题目说明 题目114. Flatten Binary Tree to Linked List,将一个二叉树"原地"压缩为"链表"形态的二叉树.难度为Medium! 二.我的解答 这个题目如果允许使用栈的话Easy,先序遍历二叉树,右子树入栈,左子树入栈.当栈不空的时候,将栈顶元素放到右子树即可. class Solution{ public: void flatten(TreeNode* root){ //先根遍历 if(root==NULL) return;

114 Flatten Binary Tree to Linked List [Python]

114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二叉树展开成链表 [[]D:\dataStructure\Leetcode\114.png] (D:\dataStructure\Leetcode\114.png "114") 思路:将根节点与左子树相连,再与右子树相连.递归地在每个节点的左右孩子节点上,分别进行这样的操作. 代码 clas

!!!!!!114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 ======== 思路: 将一个二叉树 就地 压成"链表"的结构;

Java for LeetCode 114 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6解题思路:试图通过排序后new TreeNode是无法通过的,这道题的意思是把现有的树进行剪枝操作.JAVA实现如下: static public void flatten(TreeNode root) { wh

LeetCode OJ 114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to show hints. Subscribe to see which companies asked this question 解答 先序遍历同时把节点都堆到左边,因为先序先处理左子树,所以这

8.7 114 Flatten Binary Tree to Linked List

Question: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Method: put root.left to its right, and connect root.right to its original left.right(if root.le

leetcode 114. Flatten Binary Tree to Linked List (Python版)

题目: Given a binary tree, flatten it to a linked list in-place. 算法思路: 其实该题目就是二叉树前序遍历的变形 我代码沿用leetcode 144. Binary Tree Preorder Traversal 代码: class Solution(object):     def preorderTraversal(self, root):         """         :type root: Tree

114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 法I:堆栈 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), r