0114.cpp (542B)
1 class Solution { 2 public: 3 void flatten(TreeNode *root) { 4 TreeNode *crnt = new TreeNode(-1); 5 stack<TreeNode *> st; 6 st.push(root); 7 while (!st.empty()) { 8 TreeNode *root = st.top(); 9 st.pop(); 10 crnt->right = root; 11 while (root) { 12 crnt = root; 13 if (root->right) st.push(root->right); 14 15 root->right = root->left; 16 root->left = nullptr; 17 root = root->right; 18 } 19 } 20 } 21 };