上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* 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) {
}
};
golang 解法, 执行用时: 56 ms, 内存消耗: 8.9 MB, 提交时间: 2021-06-22 17:09:10
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func convertBiNode(root *TreeNode) *TreeNode {
dummyNode := &TreeNode{}
resNode := dummyNode
var inorder func(*TreeNode)
inorder = func(node *TreeNode) {
if node == nil {
return
}
inorder(node.Left)
// 在中序遍历的过程中修改节点指向
resNode.Right = node
node.Left = nil
resNode = node
inorder(node.Right)
}
inorder(root)
return dummyNode.Right
}