LeetCode·114. Flatten Binary Tree to Linked List
程序员文章站
2022-03-07 23:39:44
...
思路:先序遍历
/**
* 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 == NULL)return;
stack<TreeNode*> st;
st.push(root);
TreeNode* fn = root;
TreeNode* fr = root;
while(!st.empty()){
fn = st.top();
if(fn != root){
fr->right = fn;
fr->left = NULL;
fr = fn;
}
st.pop();
if(fn->right != NULL)
st.push(fn->right);
if(fn->left != NULL)
st.push(fn->left);
}
}
};
推荐阅读
-
LeetCode 114.Flatten Binary Tree to Linked List (二叉树展开为链表)
-
114. Flatten Binary Tree to Linked List(二叉树展开为链表)
-
Leetcode Flatten Binary Tree to Linked List
-
【leetcode】114. 二叉树展开为链表(Flatten Binary Tree to Linked List)(DFS)
-
Flatten Binary Tree to Linked List
-
【python/M/114】Flatten Binary Tree to Linked List
-
【LeetCode】convert-sorted-list-to-binary-search-tree
-
LeetCode: 114. Flatten Binary Tree to Linked List
-
114. Flatten Binary Tree to Linked List
-
LeetCode 114 Flatten Binary Tree to Linked List