本文主要是介绍LeetCode·114. Flatten Binary Tree to Linked List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路:先序遍历
/*** 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!