[Leetcode]Flatten Binary Tree to Linked List~

原创 2015年11月20日 16:01:10

Flatten Binary Tree to Linked List My Submissions Question
Total Accepted: 66064 Total Submissions: 223714 Difficulty: Medium
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.

Hints:
If you notice carefully in the flattened tree, each node’s right child points to the next node of a pre-order traversal.

Subscribe to see which companies asked this question
中午上课前在leetcode做的时候出了各种细节错误,于是上课的时候在纸上写了下,也就十多分钟。上完课我回来提交,AC。
有时候在纸上写会更细心,写的过程也在思考。
思路:
递归处理,将左右子树Flatten,并判断左子树是否为空,为空则直接返回。不为空则将左子树的最后一个结点的右孩子设为右子树,并将左子树设置为根结点的右子树,并清空根节点左子树即可

/**
 * 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 && !root->right) return;
        flatten(root->left);
        flatten(root->right);
        if(end(root->left)){
            end(root->left) -> right = root->right;
            root->right = root->left;
            root->left = nullptr;
            return;
        }
        else{
            return;
        }
    }
    TreeNode* end(TreeNode* root){
        if(!root)   return nullptr;
        auto p = root;
        while(p->right){
            p = p->right;
        }
        return p;
    }
};

P.S 上张图记录下,这种感觉蛮好的
这里写图片描述

版权声明:转载请注明出处

相关文章推荐

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 1 ...

[Leetcode]Flatten Binary Tree to Linked List (三种方法)

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

[leetcode]114. Flatten Binary Tree to Linked List@Java解题报告

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/ Given a bin...

[LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表 C++

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

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

Leetcode: Flatten Binary Tree to Linked List

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

leetcode || 114、Flatten Binary Tree to Linked List

problem: Given a binary tree, flatten it to a linked list in-place. For example, Give...

Flatten Binary Tree to Linked List -- leetcode

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

《leetCode》:Flatten Binary Tree to Linked List

题目Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]Flatten Binary Tree to Linked List~
举报原因:
原因补充:

(最多只允许输入30个字)