Leetcode 之Flatten Binary Tree to Linked List(50)

Posted 牧马人夏峥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 之Flatten Binary Tree to Linked List(50)相关的知识,希望对你有一定的参考价值。

将左子树接到右子树之前,递归解决

void flatten(TreeNode *root)
      {
          if (root == nullptr)return;

          flatten(root->left);
          flatten(root->right);
          //如果没有左子树,直接返回即可
          if (root->left == nullptr)return;
          p = root->left;
          //寻找左子树的最后一个结点
          while (p->right)p = p->right;
          //将右结点接在左子树的最后一个结点上
          p->right = root->right;
          //将左子树移到右子树上
          root->right = root->left;
          root->left = nullptr;
      }
View Code

 

以上是关于Leetcode 之Flatten Binary Tree to Linked List(50)的主要内容,如果未能解决你的问题,请参考以下文章

[Leetcode] Flatten Binary Tree to Linked List

[LeetCode] Flatten Binary Tree to Linked List

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

LeetCode 114. Flatten Binary Tree to Linked List

leetcode Flatten Binary Tree to Linked List

leetcode--114. Flatten Binary Tree to Linked List