• 114. Flatten Binary Tree to Linked List(M)


    114. Flatten Binary Tree to Linked List
    Given a binary tree, flatten it to a linked list in-place.
    
    For example, given the following tree:
    
        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:
        TreeNode* prev = NULL;
        void flatten(TreeNode* root) {
            //using this solution: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/36977/My-short-post-order-traversal-Java-solution-for-share
            if(root == NULL)
                return;
            flatten(root->right);
            flatten(root->left);
            root->left = NULL;
            root->right = prev;
            prev = root;
            
            
            
        }
    };

  • 相关阅读:
    Linux统计文件个数
    python string与list互转
    Python中请使用isinstance()判断变量类型
    xpath提取多个标签下的text
    内存盘
    Watchdog
    渗透测试
    GMT与UTC简介
    ASN.1(抽象语法标记)
    Linux nmap
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/10327484.html
Copyright © 2020-2023  润新知