程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> leetcode_question_114 Flatten Binary Tree to Linked List

leetcode_question_114 Flatten Binary Tree to Linked List

編輯:C++入門知識

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 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.    

void flatten(TreeNode *root) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        if(root==NULL) return;  
        if(root->left == NULL && root->right == NULL) return;  
        flatten(root->left);  
        flatten(root->right);  
  
        TreeNode* tmpright = root->right;  
        root->right = root->left;  
        root->left = NULL;  
        TreeNode* tmp = root;  
        while(tmp->right)  
            tmp = tmp->right;  
        tmp->right = tmpright;  
        return;  
    }  

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved