LeetCode(面试题17.12)BiNode
作者:互联网
虽然过了,但是还是不是很清楚,中序遍历,访问每个非空节点时,将左指针置空,pre记录每个父节点,父节点的right置为当前节点
/** * 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: TreeNode* convertBiNode(TreeNode* root) { TreeNode*p = root,*pre = NULL,*newroot; stack<TreeNode*>s; bool flag = true; while(p||!s.empty()){ if(p){ s.push(p); p = p->left; }else{ p = s.top(); if(flag&&p->left==NULL){ newroot = p; flag = false; } p->left = NULL; if(pre)pre->right = p; cout<<p<<" "<<p->left<<" "<<p->right<<endl; s.pop(); pre = p; p = p->right; } } return newroot; } };
标签:pre,BiNode,面试题,right,TreeNode,17.12,NULL,节点,left 来源: https://www.cnblogs.com/stevenzrx/p/16286342.html