/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!root)
return;
stack<TreeNode*> stk;
if(root->right)
stk.push(root->right);
if(root->left)
stk.push(root->left);
TreeNode* n=root;
while(!stk.empty()){
TreeNode* tn=stk.top();
stk.pop();
n->right=tn;
n->left=NULL;
if(tn->right)
stk.push(tn->right);
if(tn->left)
stk.push(tn->left);
n=tn;
}
}
};
Leetcode: Flatten Binary Tree to Linked List
最新推荐文章于 2022-02-22 10:11:49 发布