zoukankan      html  css  js  c++  java
  • 【Flatten Binary Tree to Linked List】cpp

    题目:

    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

    代码:

    /**
     * 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;
                stack<TreeNode *> sta;
                TreeNode *dummy = new TreeNode(INT_MIN);
                TreeNode *pre = dummy;
                dummy->right = root;
                sta.push(root);
                while ( !sta.empty() )
                {
                    TreeNode *tmp = sta.top(); sta.pop();
                    if ( tmp->right ) sta.push(tmp->right);
                    if ( tmp->left ) sta.push(tmp->left);
                    tmp->left = NULL;
                    pre->right = tmp;
                    pre = tmp;
                }
        }
    };

    tips:

    先序遍历(node->left->right);每次出栈的元素都切断left(为什么right不用切?因为在下一次迭代的时候,pre->right自然就把right的值覆盖了)。

    设立一个pre节点,保存上一次出栈的元素。

    pre->right = tmp就能按照题意把原有的tree给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) {
            // terminal condition
            if (!root) return;
            // recersive left and child
            flatten(root->left);
            flatten(root->right);
            // if left is not null then do the reconnection
            if (!root->left) return;
            TreeNode *p = root->left;
            while ( p->right) p = p->right;
            p->right = root->right;
            root->right = root->left;
            root->left = NULL;
        }
    };

    ===================================================

    第二次过这道题,使用非递归版做的,一开始忘记了给left方向断开,改了一次之后AC了。

    /**
     * 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) {
                stack<TreeNode*> sta;
                TreeNode* pre = new TreeNode(0);
                if ( root ) sta.push(root);
                while ( !sta.empty() )
                {
                    TreeNode* tmp = sta.top();
                    sta.pop();
                    pre->right = tmp;
                    pre->left = NULL;
                    if ( tmp->right ) sta.push(tmp->right);
                    if ( tmp->left ) sta.push(tmp->left);
                    pre = tmp;
                }
        }
    };
  • 相关阅读:
    关于代码片段
    感谢visual studio(git插件)救我一命
    SRX一些配置命令
    .NET Core Policy和Claim
    .NET Core Identity 用户管理和策略
    .NET Core基础配置、注册服务、中间件、ViewComponent、DbContext
    table非常细边框
    js数字转大写金额
    蓝牙开发
    Laravel artisan 命令
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4505174.html
Copyright © 2011-2022 走看看