美文网首页
114. Flatten Binary Tree to Link

114. Flatten Binary Tree to Link

作者: jecyhw | 来源:发表于2019-06-14 06:41 被阅读0次

    题目链接

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

    代码

    class Solution {
    public:
        void flatten(TreeNode* root) {
            TreeNode* cur = NULL;
            dfs(root, cur);
        }
    
        void dfs(TreeNode* parent, TreeNode*& cur) {
            if (parent == NULL) {
                return;
            }
            TreeNode* right = parent->right;
            TreeNode* left = parent->left;
            if (cur == NULL) {
                cur = parent;
                cur->right = NULL;
                cur->left = NULL;
            } else {
                cur->right = parent;
                cur->left = NULL;
                cur = parent;
            }
    
            dfs(left, cur);
            dfs(right, cur);
        }
    };
    

    相关文章

      网友评论

          本文标题:114. Flatten Binary Tree to Link

          本文链接:https://www.haomeiwen.com/subject/ojmyfctx.html