您当前的位置: 首页 >  链表

对方正在debug

暂无认证

  • 4浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

二叉树展开为链表

对方正在debug 发布时间:2020-02-21 16:09:21 ,浏览量:4

题目:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/submissions/

/**
 * 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) {
        /*
        *从根节点开始遍历,如果当前点有左节点,
        *则将其对应的左子树这一块并入到其有右
        *子树上。
        */
        while(root != nullptr) {
            if(root->left != nullptr) {
                TreeNode *most_right = root->left;
                while(most_right->right != nullptr) most_right = most_right->right;
                most_right->right = root->right;
                root->right = root->left;
                root->left = nullptr;
            }
            root = root->right;
        }
    }
};
关注
打赏
1664895754
查看更多评论
立即登录/注册

微信扫码登录

0.0392s