leetcode-114. Flatten Binary Tree to Linked List
生活随笔
收集整理的這篇文章主要介紹了
leetcode-114. Flatten Binary Tree to Linked List
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
?
The flattened tree should look like:
1\2\3\4\5\6思路:遞歸處理,引用二叉鏈表的思想,使用pre記錄上一個分支的指針。
Accepted Code: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 private: 12 TreeNode* pre=nullptr; 13 public: 14 void flatten(TreeNode* root) { 15 if(root==nullptr) 16 return; 17 flatten(root->right); 18 flatten(root->left); 19 root->right=pre; 20 root->left=nullptr; 21 pre=root; 22 } 23 };
?
轉載于:https://www.cnblogs.com/hongyang/p/6423405.html
總結
以上是生活随笔為你收集整理的leetcode-114. Flatten Binary Tree to Linked List的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 因泄露三星折叠屏 爆料人被警告:相关信息
- 下一篇: MapReduce入门2-流量监控